使用 Apache mod_rewrite 检测空 REQUEST_URI 时出现问题 [英] Problem detecting empty REQUEST_URI with Apache mod_rewrite
问题描述
我正在使用这样的重定向规则运行 Apache:
I am running Apache witha redirect rule like this:
RewriteCond %{HTTP_HOST} ^1st-domain.com
RewriteRule ^(.*)$ http://2nd-domain.com$1 [R=permanent,L]
这成功地将 http://1st-domain.com 重定向到 http://2nd-domain.com但是,当 REQUEST_URI 为空时,我想重定向到第三个域.
This successfully redirects http://1st-domain.com to http://2nd-domain.com However, when the REQUEST_URI is empty, I want to redirect to a third domain.
RewriteCond %{HTTP_HOST} ^1st-domain.com$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://3rd-domain.com$1 [R=permanent,L]
但这不起作用,而是重定向到 2nd-domain.com
But this does not work and instead redirects to 2nd-domain.com
我的规则是这样排序的:
My rules are ordered like this:
RewriteCond %{HTTP_HOST} ^1st-domain.com$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://3rd-domain.com$1 [R=permanent,L]
RewriteCond %{HTTP_HOST} ^1st-domain.com
RewriteRule ^(.*)$ http://2nd-domain.com$1 [R=permanent,L]
有什么建议吗?提前致谢.
Any suggestions? Thank you in advance.
更新
- 空 REQUEST_URI:http://1st-domain.com
- 非空 REQUEST_URI:http://1st-domain.com/something
第一条规则应该将空的 request_uri 指向 3rd-domain.com,第二条规则应该将非空的 request_uri 指向 2nd-domain.com
The first rule should direct an empty request_uri to 3rd-domain.com, the second rule should direct the non-empty request_uri to 2nd-domain.com
有用的花絮您可以使用以下代码段打开 mod_rewrite 调试:
USEFUL TIDBIT You can turn on mod_rewrite debug with this snippet:
<IfModule mod_rewrite.c>
RewriteLog "/home/domain.com/logs/rewrite.log"
RewriteLogLevel 3
</IfModule>
我不知道的非常有用的调试选项.
Very useful debug option I hadn't known.
推荐答案
这应该有效:
RewriteCond %{HTTP_HOST} ^1st-domain.com
RewriteRule ^$ http://3rd-domain.com [R=permanent,L]
RewriteCond %{HTTP_HOST} ^1st-domain.com
RewriteRule ^(.+)$ http://2nd-domain.com/$1 [R=permanent,L]
希望能帮到你!
注意:REQUEST_URI 在 httpd.conf 和 .htaccess 之间略有不同,它在 httpd.conf 中以额外的反斜杠开头.这意味着在 httpd.conf 中,第一个重写规则应该是 ^/$
,而不仅仅是 ^$
.
Note: REQUEST_URI is slightly different between httpd.conf and .htaccess, it starts with an extra backslash in httpd.conf. This means that in httpd.conf the first rewrite rule should be ^/$
, not just ^$
.
这篇关于使用 Apache mod_rewrite 检测空 REQUEST_URI 时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!