Forum Moderators: phranque

Message Too Old, No Replies

Rewrite request from a specific referrer

         

dan121

2:56 pm on Apr 25, 2005 (gmt 0)

10+ Year Member



I would like to redirect requests for one of my subdomains (xyz.example.com) to my primary domain (example.com), but only from ONE specific referrer subdomain (xyz.example2.com).

I assume the way to go about this is with a mod_rewrite statement in my htaccess file on xyz.example.com, but I'm not sure what the statement should be:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^xyz.example2.com [NC]
RewriteRule? [example.com...] [NC]

Any help would be greatly appreciated!
Thanks.

jd01

10:27 pm on Apr 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi dan121,

It looks like you are very close some simple 'tweeks' should get you there:

RewriteEngine On

RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^xyz\.example2\.com [NC]
RewriteRule . [example.com...] [L]

The rule will now check any requested page. The first condition chacks to see if there is a referer present. If present, the referer will be checked against the second condition.

backslashing the .(dot) makes sure they are compared as .(dot) not 'any characted except a line break'

Overall you were very close, you might need to adjust for your specific situation, but this should be right about there.

Hope this helps.

Justin

Edited: can't type

sitz

1:37 am on Apr 26, 2005 (gmt 0)

10+ Year Member



Note that the Referer: header generally includes the protocol as well (which can be either http or https. *Can* be a couple of other things, but 'https?' will handle 99.9+% of the cases), so your RewriteCond would be better written as:

RewriteCond %{HTTP_REFERER} ^(https?://)?xyz\.example2\.com [NC]

...to handle this eventuality. If you want to be REALLY pedantic:


RewriteCond %{HTTP_REFERER} ^(https?://)?xyz\.example2\.com[^.] [NC]

This ensures that you don't Rewrite requests coming from, say: [example.com.mx...] (You don't need to escape the '.' in a bracket expression; most special characters lose their special characteristics when placed within brackets. See your local regex man page for details).