-->

Selectively redirect to a new domain using apache

2019-08-22 11:36发布

问题:

This is a followup question on Redirect to a new domain using apache vhost configuration

I want my requests from

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

to be redirected to

someotherdomain.com/dell-inspiron-15

but selectively for some products which are whitelisted.

For example for products :

  1. dell-inspiron-15.
  2. dell-inspiron-16.
  3. dell-inspiron-17

redirect to the new URL but for any other products I want to use the current path itself.

Currently my vhost configuration looks like below. It redirects to someotherdomain for all the products in query parameter.

Listen 12567
NameVirtualHost *:12567

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

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

Questions:

  1. As asked above, how can I selectively redirect?
  2. Given that my list of whitelisted products is small (maybe some 10-11 items) what is an ideal place to store this whitelisted products and read in vhost and how.

Any leads here is really appreciated.

回答1:

Since you have a small number of models, you could just list them out, like so:

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]

By default, RewriteCond lines are interpreted with the AND operation between each. So each condition must be TRUE before the RewriteRule is triggered. Here use the OR operator between the lines. As long as one matches, you are ok.