Solved

.htaccess rewrite based on referrer

Posted on 2006-10-30
7
444 Views
Last Modified: 2012-05-05
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).
0
Comment
Question by:FartingUncle
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:HackneyCab
ID: 17835343
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.
0
 
LVL 1

Author Comment

by:FartingUncle
ID: 17835361
I have also tried the following patterns instead of !^/$

!^$
^.+$
^..+$
^.+
^..+
.+
..+

All gave the same result.
0
 
LVL 1

Author Comment

by:FartingUncle
ID: 17835418
(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).

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

    http://www.example.com/test
    => 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...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 16

Accepted Solution

by:
HackneyCab earned 125 total points
ID: 17838602
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.
0
 
LVL 1

Author Comment

by:FartingUncle
ID: 17839479
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 => 127.0.0.1, though obviously with my own settings), but you've solved it for me.  Thank you so much!
0
 
LVL 16

Expert Comment

by:HackneyCab
ID: 17841106
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.
0
 
LVL 1

Author Comment

by:FartingUncle
ID: 17841710
*grin*
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hosting a website using port 443 SSL CA using Wampserver 2 How to: 6 53
How to analyze web traffic logs 10 107
Ubuntu 14 with Apache 7 75
Apache error.log 19 42
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question