在文档根目录中尝试以下操作:
RewriteEngine On
# check if subdomain folder has the existing file, if so, serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f
RewriteRule ^(.*)$ /%1/$1 [L]
# check if subdomain folder has the existing directory WITH A TRAILING SLASH, if so serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*?)/$ /%1/$1/ [L]
# check if subdomain filder has the existing directory but missing trailing slash, redirect with it
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /$1/ [R=301,L]
这个方法为你做了两件事,如果它是404,它不会尝试内部重写,所以如果你去:
http://sub.domain.co.uk/this/path/does/not/exist/blah
,您不会收到一条404消息:
/sub/this/path/does/not/exist/blah
不存在,只是
/this/path/does/not/exist/blah
因为URI没有被重写,因此没有暴露您的底层目录结构。
第二件事是,你可能有
DirectorySlash
已打开。这意味着如果您访问以下目录:
http://sub.domain.co.uk/this/path
,缺少尾部斜杠,mod_dir将希望重定向并添加该斜杠,但它可能会出错,因为URI已被重写,并将您重定向到:
http://sub.domain.co.uk/sub/this/path/
。我们预先添加了带有正确URI的尾部斜杠,以隐藏
sub
。