简体   繁体   English

相同网址的Apache重定向规则

[英]Apache redirect rule for the same url

I am trying to write a RewriteCond and RewriteRule to the following. 我正在尝试将RewriteCondRewriteRule编写为以下内容。

If someone tries to access forex.com or forexample.com they should both redirect to 如果有人尝试访问forex.com或forexample.com,则他们都应重定向到

forexample.com/abcd/xyz/

The below rewrite condition works for forex.com 以下重写条件适用于forex.com

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

But when I add another rule like below it goes to a redirect loop 但是,当我添加如下所示的另一条规则时,它将转到重定向循环

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

Any help will be greatly appreciated. 任何帮助将不胜感激。

This works because forex.com and forexample.com are different domains: 这是有效的,因为forex.comforexample.com是不同的域:

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

But in the second case, I believe the issue is that the rule as you have set will redirect anything on https://forexample.com/ to https://forexample.com/abcd/xyz/ even if it is already in https://forexample.com/abcd/xyz/ : 但是在第二种情况下,我认为问题在于您设置的规则会将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]

So do a check for the REQUEST_URI not being abcd/xyz/ and you should be good to go: 因此,请检查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]

But then looking this over, the first HTTP_HOST rule makes little sense. 但是,再HTTP_HOST ,第一个HTTP_HOST规则就没有意义了。 Perhaps you want it to just be this: 也许您希望它就是这样:

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

A good way to debug stuff like this is to use curl -I from the command line. 调试此类内容的一种好方法是从命令行使用curl -I That command will show you the exact HTTP headers you getting based on a rule or request. 该命令将向您显示根据规则或请求获取的确切HTTP标头。 So use it like this; 所以像这样使用它; using my MAMP test environment for an example: 以我的MAMP测试环境为例:

curl -I http://localhost:8888

And the output for me would be: 对我来说输出是:

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

Note the HTTP/1.1 302 Found which means it's a temporary redirect and the Location: http://localhost:8888/abcd/xyz/ which indicates where the rule is rewriting the call to http://localhost:8888 as. 请注意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