【问题标题】:Trouble redirecting subdomains to a folder无法将子域重定向到文件夹
【发布时间】:2012-08-03 15:35:13
【问题描述】:

我已经搞定了:

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] 

但这似乎根本不起作用。

任何帮助将不胜感激。

伊恩

【问题讨论】:

  • 我不确定,它们在我使用的示例中。

标签: mod-rewrite subdomain vhosts wildcard-subdomain


【解决方案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 已被重写并将您重定向到:@987654327 @。我们先发制人地添加带有正确 URI 的尾部斜杠,隐藏了 sub

【讨论】:

  • 嘿,这似乎不起作用,如果显示任何错误,我可以尝试调试它吗?我在最后向 Google 添加了一个重写规则,该规则被执行,因此它实际上不满足任何条件。
  • @IanJamieson 是否匹配主机?
  • 是的,它与主机匹配,我通过向 Google 添加重定向来测试它是否匹配 HTTP 主机,确实如此!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 2014-09-05
  • 2012-11-23
  • 1970-01-01
相关资源
最近更新 更多