Fallback - redirect to a new domain using apache vhost configuration

半世苍凉 提交于 2019-12-11 15:23:58

问题


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.



来源:https://stackoverflow.com/questions/47548981/fallback-redirect-to-a-new-domain-using-apache-vhost-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!