0

我有这个工作:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteRule ^(.*) http://domain.co.uk/%1 [L]

所以它会将 test.domain.co.uk 重定向到 test.domain.co.uk/test

但是,我希望它更改文档根目录而不是用户看到重定向。我在这里阅读了几篇文章并尝试了一些事情。但我不确定如何调试任何错误,它只是不起作用。我尝试过的另一个:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/-%1 -d
RewriteRule ^(.*)$ -%1/$1 [L] 

但这似乎根本不起作用。

任何帮助,将不胜感激。

伊恩

4

1 回答 1

1

在您的文档根目录中尝试这样的操作:

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 是' t 重写,因此不会暴露您的底层目录结构。

第二件事是你可能已经DirectorySlash打开了。这意味着如果您访问类似的目录:http://sub.domain.co.uk/this/path,缺少尾部斜杠, mod_dir 将希望重定向并添加该斜杠,但它可能会做错,因为 URI 已被重写并将您重定向到:http://sub.domain.co.uk/sub/this/path/。我们先发制人地添加带有正确 URI 的尾部斜杠,隐藏sub.

于 2012-08-06T16:36:13.177 回答