Forum Moderators: phranque

Message Too Old, No Replies

301 redirect OR RewriteRule

         

aha7

2:32 am on Apr 26, 2007 (gmt 0)

10+ Year Member



Hello,

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?

jdMorgan

11:54 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this:

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]

However, you need to handle two additional cases:
1) What should happen when the HTTP_REFERER is blank?
2) What should happen if the requestor is a search engine robot?

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

aha7

10:18 am on Apr 27, 2007 (gmt 0)

10+ Year Member



"1) What should happen when the HTTP_REFERER is blank?
2) What should happen if the requestor is a search engine robot?"

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.

jdMorgan

1:02 pm on Apr 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that these requirements were not explicitly stated above. Since the default "else" clause does what you describe, no further coding is needed. But problems must be completely described if they are to be correctly addressed by mod_rewrite --or any other programming language-- so I wanted to be sure these cases were covered.

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