同一网址的Apache重定向规则

时间:2014-06-28 01:32:58

标签: apache mod-rewrite redirect

我正在尝试将RewriteCondRewriteRule写入以下内容。

如果有人试图访问forex.com或forexample.com,他们都应该重定向到

forexample.com/abcd/xyz/

以下重写条件适用于forex.com

RewriteCond %{HTTP_HOST} ^forex\.com [NC]
RewriteRule ^(.*)$ https://forexample.com/abcd/xyz/ [R,NE]

但是当我添加另一条规则时,它会转到重定向循环

RewriteCond %{HTTP_HOST} ^forexample\.com [NC]
RewriteRule ^(.*)$ https://forexample.com/abcd/xyz/ [R,NE]

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这是有效的,因为forex.comforexample.com是不同的域:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^forex\.com [NC]
RewriteRule ^(.*)$ https://forexample.com/abcd/xyz/ [R,NE]

但在第二种情况下,我认为问题在于,您设置的规则会将https://forexample.com/上的任何内容重定向到https://forexample.com/abcd/xyz/,即使它已经在https://forexample.com/abcd/xyz/中:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^forexample\.com [NC]
RewriteRule ^(.*)$ https://forexample.com/abcd/xyz/ [R,NE]

请检查REQUEST_URI不是abcd/xyz/,你应该好好去看看:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^forexample\.com [NC]
RewriteCond %{REQUEST_URI} !^/(abcd/xyz/.*)$ [NC]
RewriteRule ^(.*)$ https://forexample.com/abcd/xyz/ [L,R,NE]

但是接下来看,第一个HTTP_HOST规则没什么意义。也许你希望它只是这样:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(abcd/xyz/.*)$ [NC]
RewriteRule ^(.*)$ https://forexample.com/abcd/xyz/ [L,R,NE]

调试这样的东西的好方法是从命令行使用curl -I。该命令将根据规则或请求向您显示您获得的确切HTTP标头。所以像这样使用它;以我的MAMP测试环境为例:

curl -I http://localhost:8888

我的输出是:

HTTP/1.1 302 Found
Date: Sat, 28 Jun 2014 02:19:39 GMT
Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8y DAV/2 PHP/5.4.10
Location: http://localhost:8888/abcd/xyz/
Content-Type: text/html; charset=iso-8859-1

请注意HTTP/1.1 302 Found,这意味着它是临时重定向,Location: http://localhost:8888/abcd/xyz/表示规则将http://localhost:8888的调用重写为。{/ p>