How to redirect based on referer

Have you ever wanted to redirect visitors to a special promo page based on how they were referred to your site?

You can enhance the visitor experience by checking the referer log for the visitor and then showing them a custom page that relates to the web site that referred them to you.

Why redirect visitors?

This is a good technique to use if you do advertising or link campaigns with other sites.

Let’s say that you took out an ad on Elliot’s blog and you wanted to direct any visitors who clicked on your ad to a special sales page.

On the sales page, you could include content thanking them for visiting from Elliot’s site, tell them about a special promotion for visitors referred by Elliot etc. It personalizes the whole process and makes the visitors feel appreciated.

And if they feel that their business is appreciated, they are more likely to buy your product.

The redirect code

Here is the code to check your web server referer and then redirect the visitor to your custom sales page. Simply take the two lines of code and put them in your .htaccess file and you’re done.

RewriteCond %{http_referer} ^http://([^.]+\.)*(elliotsblog)\.com
RewriteRule ^$ special.html [R=302,L]

In this example, we look for referer’s from and we direct them to a page on our site called special.html.

You should change the domain and the page to anything you like.

NOTE: You need to have the apache module mod_rewrite installed on your web server.

Enjoy !


2 Responses to “How to redirect based on referer”

  1. Good tip, however I am not quite sure why you would not point the ad directly to your special landing page directly, instead of adding a short delay with the direct. For tracking purposes all ad links should point to different URLs anyways.

    What is nice is that you can use similar code to redirect visitors based on other browser settings, i.e. the language their browser is set to.

    Thanks for sharing,

  2. Richard says:


    The handy thing about using the referer for the redirect is that if you also have text links from the site, you can redirect them as well.

    For example, perhaps you run a banner ad on a blog and the blog owner also writes a post mentioning your site and gives you a backlink to the main page of your site, with the referer redirect, you can capture the visitors who come from the blog post as well.