nac*_*ito 34

.htaccess指令适用于该目录及其所有子目录,因此您应禁止在DocumentRoot中访问,

http://sub.mydomain.com/.htaccess:

Order deny,allow
Deny from all
Run Code Online (Sandbox Code Playgroud)

并在您希望允许访问的任何特定子目录中覆盖它,

http://sub.mydomain.com/test/.htaccess:

Order allow,deny
Allow from all
Run Code Online (Sandbox Code Playgroud)

  • 在许多子目录的情况下我们应该做些什么? (6认同)

SW4*_*SW4 8

如何在根目录下使用.htaccess,并使用以下行?

RewriteEngine On
# check if request is for subdomain
RewriteCond %{HTTP_HOST} ^sub.mydomain.com$ [NC]
# check if  'test' isnt part of request
RewriteCond %{REQUEST_URI} !^/test/?(.*)$ [NC]
# if subdomain and no 'test' part, redirect to main domain...
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R,L]
Run Code Online (Sandbox Code Playgroud)

因此,如果'/ test /'部分存在,则不会发生重定向...

  • NC表示不区分大小写,R =重定向,L =最后(在此之后停止从htaccess应用规则) (3认同)