Fallback - redirect to a new domain using apache v

2019-08-19 06:25发布

问题:

This is a followup question on https://stackoverflow.com/posts/47547418

I wanted my requests from

somedomain.com/loadproduct?product=dell-inspiron-15

to be selectively redirected to

someotherdomain.com/dell-inspiron-15

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 adding /old in the url.

For example if users uses

somedomain.com/old/loadproduct?product=dell-inspiron-15

then they should not be redirected to someotherdomain but should be served through a valid url somedomain.com/loadproduct?product=dell-inspiron-15

but if they use

somedomain.com/loadproduct?product=dell-inspiron-15

they should be redirected.

Currently my vhost configuration looks like below. It redirects to someotherdomain for selected products but there is no fallback configuration.

Listen 12567
NameVirtualHost *:12567

<VirtualHost *:12567>
    ServerName somedomain.com
    ProxyPreserveHost On

    RewriteEngine On
    RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-15) [NC,OR]
    RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-16) [NC,OR]
    RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-17) [NC]
    RewriteRule ^/?loadproduct$ http://someotherdomain.com/%1? [R=301,L,NC]
</VirtualHost>

Any leads here is really appreciated.

回答1:

Modify your RewriteCond conditions and add one where it says not to accept /old/ in the path (URI). So:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/old/.*$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-15) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-16) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-17) [NC]
RewriteRule ^/?loadproduct$ http://someotherdomain.com/%1? [R=301,L,NC]

In the first line, no need to specify [AND], as it is implicit. The ! character negates the match.

Disclaimer: I have not tested this on a real Apache, but I am pretty sure it is ok.