mod_rewrite:通过%{HTTP_HOST}将子域重写到目录时出错

时间:2013-02-11 02:59:18

标签: .htaccess mod-rewrite subdomain

mod_rewrite

的问题

对于Apache的mod_rewrite,我遇到的麻烦比我想象的要多。我已经发布了一个问题 问题,这已经澄清,但我继续得到错误,我无法看到任何逻辑错误 组态。任何帮助将不胜感激!

我使用的.htaccess如下:

# Begin Rewrite Module for http://*.example.com/
# ==============================================
<IfModule mod_rewrite.c>

    # Turn the rewrite engine on and set the base path.
    RewriteEngine On
    RewriteBase /

    # Map subdomains to their respective directories.
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
    RewriteRule ^(.*)$ public_subdomains/%1/$1 [L]

</IfModule>

这不起作用,因为内部重定向太多,导致500 Internal Server Error - 这是它的调试日志:

[Fri Feb 15 16:49:30.318509 2013] [core:error] [pid 2316:tid 1708] [client 127.0.0.1:9141] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3502): [client 127.0.0.1:9141] AH00121: r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /public_subdomains/sub1/index.php
[Fri Feb 15 16:49:30.318509 2013] [core:debug] [pid 2316:tid 1708] core.c(3508): [client 127.0.0.1:9141] AH00122: redirected from r->uri = /

我找到了一种解决方案,通过替换RewriteRule上[L]标志的[END]标志,它完美地将请求URI精确映射到子文件中的相应文件-domain目录。

不幸的是,它太完美了。对subdomain.example.com/folder/的调用会在不考虑DirectoryIndex的情况下重写为example.com/public_subdomains/subdomain/folder/,而应将其重写为example.com/public_subdomains/subdomain/folder/index.php(假设index.php设置为DirectoryIndex)。

如果有人能帮助我阐明这一点,我将永远感激不尽!

1 个答案:

答案 0 :(得分:4)

好吧,有一个逻辑错误。让我向您介绍docs

中精彩的控制流程图

Control flow diagram about the workings of mod_rewrite

你看,内部重定向也是一个请求,所以它再次由任何和所有重写规则处理。因此,假设用户请求something.example.com/x:RewriteRule macthes,RewriteCond已完成,因此内部重定向到public_subdomains / something / x。新的请求是对something.example.com/public_subdomains/something/x:RewriteRule匹配,RewriteCond已经完成......并且你有无限循环。

[L]标志仅停止处理当前请求:无论如何,请求将在第二遍中处理。 [END]标志确实会停止循环,但它是一个暴力的东西,因为你已经注意到它有副作用,所以最好加入一个停止递归的条件。像这样:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/public_subdomains/%1
RewriteRule ^(.*)$ public_subdomains/%1/$1 [L]

添加的RewriteCond将检查请求的uri是否已经以“public_subdomains / something”开头。

这是未经测试的,所以可能有问题,但这是基本的想法。