0

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.

ThinkGeek
  • 4,749
  • 13
  • 44
  • 91

1 Answers1

0

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.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • But /old is not a valid url, it should redirect to a url without /old – ThinkGeek Nov 29 '17 at 09:38
  • In your question you say if `/old/` is present, do not redirect. So the line I added is to make sure the RewriteRule is not applied to a request that contains `/old/`. I misunderstood the question or? – Nic3500 Nov 29 '17 at 09:41
  • I tried changing question language a bit, can you please check now? – ThinkGeek Nov 29 '17 at 10:01
  • Actually what I meant was use old in the url as a marker for fallback. – ThinkGeek Nov 29 '17 at 10:04
  • I added RewriteCond %{QUERY_STRING} (?:^|&)product=(ENGY) [NC,OR] so as expected "?loadproduct=ENGY" => Should be redirected but I can see url with "?loadproduct=ENGYHyd" is also getting redirected. Any idea why? Is it doing a prefix match? – ThinkGeek Dec 04 '17 at 12:11