Forum Moderators: phranque
I want to redirect all visitors to a specific URL on my site to another page using 301 redirect UNLESS the visitor is coming from one specific domain.
Example: Assume that I want to 301-redirect all visitors from "http://mysite.com/f1/page.php" to "http://mysite.com/f1/newpage.php" unless the visitor came to "http://mysite.com/f1/page.php" from "http://myothersite.net/page/" in which case I want to send him to "http://mysite.com/f1/welcomefriend.php".
I know that the 301 part can be done by the following:
redirect 301 /f1/page.php [mysite.com...]
and I know that the second part can be done by the following (not so sure, though):
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myothersite\.net/ [NC]
RewriteRule page\.php$ welcomefriend.php [L]
Now how can I put these to work together?
RewriteEngine on
#
# Rewrite to welcome if referrer is myothersite
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myothersite\.net [NC]
RewriteRule page\.php$ /welcomefriend.php [L]
# Else rewrite to newpage
RewriteRule page\.php$ /newpage.php [L]
Blank referrers are common, and DO NOT indicate that the requestor is 'bad' -- Many are people behind corporate and ISP caching proxies (such as all AOL users). Others are using "internet security" software which suppresses the referrer information by default, but they are not even aware of this. SE robots also provide no referrer, but you may wish to handle them differently.
So what should happen in those two cases?
Jim
I thought that in these two cases the condition would be false and the second RewriteRule would execute returning newpage.php when page.php is requested. Did I miss something?
If I was wrong, them what should I do to redirect the two cases above from page.php to newpage.php?
i.e. for any request that was not referred by myothersite.net, return newpage.php when it asks for page.php.
It can be a bit of a nasty shock when you realize that the code does exactly what you told it to do, and not at all what you wanted... :)
Jim