I am trying to write a RewriteCond
and RewriteRule
to the following.
If someone tries to access forex.com or forexample.com they should both redirect to
forexample.com/abcd/xyz/
The below rewrite condition works for 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:
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/
:
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:
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. 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. That command will show you the exact HTTP headers you getting based on a rule or request. So use it like this; using my MAMP test environment for an example:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.