Reputation: 5137
Old Domain: abc.com
New Domain: def.com
I wanted my requests from abc.com/loadsite.action?site=eu to be redirected to def.com/eu but at the same time, I want to make sure if something goes wrong with the new domain, users are still able to use old domain by some means (maybe by adding /old in the URL, abc.com/old/loadsite.com?site=eu).
Currently my vhost configuration looks like below. It redirects to def.com for selected marketplaces but there is no fallback configuration.
Listen 12567 NameVirtualHost *:12567
ServerName abc.com ProxyPreserveHost On
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)site=(in) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)site=(jp) [NC]
RewriteRule ^/?loadsite$ http://def.com/%1? [R=301,L,NC]
How can I redirect abc.com/old/loadsite.action?site=eu to abc.com/loadsite.action?site=eu without getting into cycle?
Any leads here is really appreciated.
Upvotes: 0
Views: 60
Reputation: 10889
Something like this should work:
RewriteCond %{THE_REQUEST} !\s/old/
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)site=(in) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)site=(jp) [NC]
RewriteRule ^/?loadsite$ http://def.com/%1? [R=301,L,NC]
RewriteRule ^/?old/(.*) /$1 [L,NC]
The last directive will simply (internally) rewrite abc.com/old/loadsite.action?site=eu
to abc.com/loadsite.action?site=eu
. Since %{THE_REQUEST} contains full HTTP request line sent by the browser to the server, even after this internal rewrite we can still use this variable to prevent rewrite to new site.
So, RewriteCond %{THE_REQUEST} !\s/old/
adds one more condition: if the original request was beginning with /old/
, do not rewrite to new site.
Upvotes: 1