[英]Apache RewriteRule is not working with single input
我在通过 HTTP 主机名选择目录时遇到问题。
我有这样的文件结构:
.
+-- .htaccess
+-- domains
+-- 123456.a12.whatever.net
| +-- .htaccess
| +-- ...
+-- example.org
| +-- .htaccess
| +-- ...
+-- subdomain.example.org
+-- index.html
根目录中的.htaccess
文件如下所示:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.*)$
RewriteRule (.*) %{REQUEST_SCHEME}://%2/$1 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/domains/%{HTTP_HOST} !-d
RewriteRule (.*) $1 [R=404,L]
RewriteCond %{DOCUMENT_ROOT}/domains/%{HTTP_HOST} -d
RewriteRule (.*) domains/%{HTTP_HOST}/$1 [DPI]
奇怪的是它适用于123456.a12.whatever.net
和example.org
但不适用于subdomain.example.org
。 当我尝试访问它时,我收到 500 内部服务器错误。 我确信这是.htaccess
的问题,因为subdomain.example.org
实际上只是index.html
。 此外,错误似乎在最后一行。
当我在本地主机上玩它时,我发现它出于某种原因开始循环。 日志说:
[core:error] 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., referer: http://subdomain.example.org/
[core:debug] AH00121: r->uri = /domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/favicon.ico, referer: http://subdomain.example.org/
[core:debug] AH00122: redirected from r->uri = /domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/domains/subdomain.example.org/favicon.ico, referer: http://subdomain.example.org/
...
[core:debug] AH00122: redirected from r->uri = /domains/subdomain.example.org/favicon.ico, referer: http://subdomain.example.org/
[core:debug] AH00122: redirected from r->uri = /favicon.ico, referer: http://subdomain.example.org/
尽管我不知道它为什么会循环,但我找到了两种不同的解决方案。
.htaccess
我认为最正确的解决方案是@anubhava 建议的解决方案。 只需将最后一行更改为RewriteRule !^domains/ domains/%{HTTP_HOST}%{REQUEST_URI}
就足够了,我认为它实际上更正确。
.htaccess
奇怪的是,另一种解决方案是将包含指令RewriteEngine On
.htaccess
添加到subdomain.example.org
。 我认为这就是其他域起作用的原因。 我不知道这的确切原因,但它有效。
您实际上可以通过使用此否定条件规则进一步简化它:
RewriteRule !^domains/ domains/%{HTTP_HOST}%{REQUEST_URI} [L,NC]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.