.htaccess rewrite based on referrer

Hi, I am trying to create rewrite rules to acheive the following (using example.com as an example):

1) Any sub-domain of example.com (including no sub-domain) is resolved to www.example.com
2) Any request that does not come from an in-site link (i.e. the referrer is not www.example.com) is redirected to the homepage (http://www.example.com/)
3) Any request that IS from an in-site link passes through and is served normally.
4) I need to be able to add further rewrite rules that will operate just on the requests that have passed through from (3).

Here's what I have so far:

      # Enable rewrite engine
      RewriteEngine On

      # Rewrite any sub-domain requests to use www.example.com
      RewriteCond      %{HTTP_HOST}            !^www\.example\.com$ [NC]
      RewriteRule      .*                        http://www.example.com/ [R,L]

      # If the referrer is not www.example.com and the request is not for the homepage,
      # redirect to the homepage
      RewriteCond      %{HTTP_REFERER}            !http://www\.example\.com/ [NC]
      RewriteRule      !^/$                        http://www.example.com/ [R,L]

Using the above, the initial rule seems to work, and the address bar updates as expected, but the second rule is causing problems.  Firefox quickly reports "Firefox has detected that the server is redirecting the request for this address in a way that will never complete." whilst IE5.5 struggles on gamely for quite a while before giving up and presenting a blank page.  Clearly I have a redirect loop going on here.

The final rewrite rule (rewrite any calls that aren't directly to the homepage) should only match request that were external referrals (including typing in the address bar or clicking on a bookmark) that are NOT to the homepage, however it seems to be matching ALL requests and causing the loop.  I don't quite understand this.

How can I fix this to do what I want? I am using Apache/1.3.29 (Unix).
Who is Participating?
HackneyCabConnect With a Mentor Commented:
Okay, this is a bit of a burn. Sometimes REQUEST_URI is simply / and sometimes it gets changed to /index.php (on my test rig, it may be /index.html on your rig).

This works for me, though:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !http://127\.0\.0\.1/testarea [NC]
RewriteCond %{REQUEST_URI} !^/testarea/(index.php)?$
RewriteRule .* http://127\.0\.0\.1/testarea/ [R,L]

(my testarea on my local machine).

The first condition requires that the referrer is not a page on our machine. The second condition requires that the REQUEST_URI is neither the root (well, /testarea is the root in my case, yours will be simply / instead), nor the directory index page (usually index.html, but index.php in my case).

If both conditions are met, then the request is redirected to the root (/testarea/ in my case) no matter what request the user makes.
I believe this is happening because the HTTP_REFERER is passed as-is by the redirect. It is not updated when mod_rewrite goes to work.

So your second rule says: referrer is empty, so redirect to the root (with an empty referrer) and test again. So it goes round and round.

Try adding this condition, so that requests for the root are ignored by the rewrite:

RewriteCond %{REQUEST_URI} !^/$

Then the redirect to http://www.example.com/ should mean that this new RewriteCond is not met, so the rewrite doesn't happen a second time.
FartingUncleAuthor Commented:
I have also tried the following patterns instead of !^/$


All gave the same result.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

FartingUncleAuthor Commented:
(previous comment posted before reading response from HackneyCab)

OK - I added that line between the last RewriteCond and RewriteRule, but got the same result.  Are you sure that REQUEST_URI is updated after the redirect?

I don't see why it would matter if the referrer is unaltered by the redirect (actually, that's the desired behaviour), as the pattern in the RewriteRule line should block it (whether the first request or as the result of a redirect).

    => http://www.example.com/ (via first rule)

    => http://www.example.com/  (via second rule because of bad referrer)

    http://www.example.com/  (no redirect)

In all 3 examples, the RewriteRule should stop any further rewriting after a request for/redirect to the homepage has occurred - I don't understand why that isn't happening...
FartingUncleAuthor Commented:
Fantastic!  I made a few tweaks (replace index.php with index\.php on line 3, and remove the slashes from line 4 (127\.0\.\1 =>, though obviously with my own settings), but you've solved it for me.  Thank you so much!
How did I manage to get the slashes all in the wrong place? That's what haste does to me.

Glad it works for you now.
FartingUncleAuthor Commented:
All Courses

From novice to tech pro — start learning today.