繁体   English   中英

相同网址的Apache重定向规则

[英]Apache redirect rule for the same url

我正在尝试将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]

任何帮助将不胜感激。

这是有效的,因为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 ,第一个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的调用重写为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM