Apache Rule to verify referrer website

Posted on 2013-09-25
Medium Priority
Last Modified: 2013-10-18
Hello, we want see if we can apply a rule in our apache which would detect which website the user is coming from to determine what type of access they have to the landing site. Based on if they come from a site we approve they would see secure items through a private key that we would like to use. If they are not coming from a site that we accept it would not use the private key.
Question by:DancingFighterG
LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 39522108
Not sure about the Apache part, but in PHP you can check $_SERVER['HTTP_REFERER'] and you will mostly be OK.  This information is set by the client browser, so you need to be aware that it's quite hackable if the attacker uses cURL.

A more reasonable approach might be PHP client authentication.  This article explains it.
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39522307
I don't think having Apache check the referrer is a good idea because it would be checking everything including javascript, images, CSS, and anything else you may be serving up.  I suspect that even if you can do it, it would slow down your server a lot.  In addition, the Apache directions recommend strongly against doing lookups for exactly that reason.  It is very time and resource consuming.
LVL 26

Expert Comment

ID: 39526339
Per the posts above it would be far cleaner to check the referer in the code, and set a cookie of two and ad some conditional logic to your landing page, but you could hack something together, with two copies of the page and a few rewrite rules e.g.

RewriteEngine On

# Redirect requests for the index.html to  /restrictedPage.html, if the request was referred from any one of a list of sites sites:
RewriteCond %{HTTP_REFERER}   ^http://(www\.)?(site1|site2|site2)\.com   [NC]
RewriteRule  index.html    /restrictedPage.html           [L]

# Only permit access to /restrictedPage.html if referred internally from the site, or from the list
RewriteCond %{HTTP_REFERER}   !^http://(www\.)?yoursite\.com   [NC]
RewriteCond %{HTTP_REFERER}   !^http://(www\.)?(site1|site2|site2)\.com   [NC]
RewriteRule restrictedPage.html  -          [F,L]

Open in new window


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

624 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