2012-08-06 127 views
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] 

但这似乎并不工作。

任何帮助,将不胜感激。

伊恩

+0

'-'的用途是什么? – 2012-08-06 16:24:05

+0

我不确定,他们在我使用的示例中。 – 2012-08-07 07:59:38

回答

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没有被重写,因此不公开您的基础目录结构。

第二件事是你可能打开了DirectorySlash。这意味着如果你访问一个目录,如:http://sub.domain.co.uk/this/path,缺少斜线,mod_dir会重定向并添加该斜线,但它可能会做错,因为URI已被重写并将重定向到:http://sub.domain.co.uk/sub/this/path/。我们先发制人地在后面加上正确的URI来隐藏sub

+0

嘿,这似乎并没有工作,有无论如何,我可以尝试和调试,如果有任何错误显示? 我在最后添加了一条重写规则,它被执行,所以它实际上并不符合任何条件。 – 2012-08-07 07:56:34

+0

@IanJamieson是房东配套吗? – 2012-08-07 16:26:36

+0

是的,它匹配的主机,我测试通过添加一个重定向到谷歌,如果它匹配的HTTP主机,它! – 2012-08-09 09:56:54

相关问题