go top

Redirect based on referrer or IP address

Using PHP you can easily redirect your website visitors to a different page depending on where they come from. For example you may have 2 different web sites selling a product but only one customer care web site where people submit support tickets. Depending from which of the 2 web sites a visitor comes to that support site you may want to show them different web page.

PHP stores information about the referring URL in one of its global variables - $_SERVER. You can easily access that value using the $_SERVER['HTTP_REFERER']. Now what you have to do is to see if any of the 2 domains is in that HTTP_REFERER variable. You can do this using he preg_match function.

This is the code that you can use:

$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/site1.com/",$referrer)) {
header('Location: http://www.customercare.com/page-site1.html');
} elseif (preg_match("/site2.com/",$referrer)) {
header('Location: http://www.customercare.com/page-site2.html');
} else {
header('Location: http://www.customercare.com/home-page.html');

Note, that because of using the header() function that code has to be put on the very top of your PHP page. Header redirect will throw an error if there is something sent to visitors browser before calling it.

Instead of using $_SERVER['HTTP_REFERER'] you can use $_SERVER['REMOTE_ADDR'] which carries information about visitors IP address. This way you can redirect users based on their IP address. If you detected that someone is trying to hack your web site you can use that PHP redirect to send him to a 'Thank you page'.

$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/",$visitor)) {
header('Location: http://www.yoursite.com/thank-you.html');
} else {
header('Location: http://www.yoursite.com/home-page.html');
  • Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.

    Free scripts
  • PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!

    Commercial PHP scripts

32 Comments to "Redirect based on referrer or IP address"

  • Muhammad Imran

    Muhammad Imran

    September 19, 2015 at 06:22 am

    Can I redirect other site visitor on my site at this way??

  • mere mehboob

    mere mehboob

    September 8, 2015 at 12:20 pm

    how to block all ip adresses from pakistan and china

    i don't want the stupid people to see my website !

  • enmanuel corvo

    enmanuel corvo

    November 7, 2014 at 08:18 am

    Here is my version of PHP redirect.


  • Ionut


    October 17, 2012 at 15:22 pm

    Can you make this with GEO ip verification ?

    echo '<a href="http://www.jocuricubataie.info"></a>';

  • Charlie Hestler

    Charlie Hestler

    September 18, 2012 at 03:35 am

    I like this code, but I have more than just one IP address to block. Is there a way to have several IP addresses blocked, instead of just one?

  • Sipylus.Net


    November 20, 2011 at 00:41 am

    I like what Asela is thinking. Is that possible?
    That way I can redirect proxy sites as the visitor
    switches and returns.

  • lakshmi


    November 5, 2011 at 13:55 pm

    Actually in my database 9 images are there.I want to display 1st 3(1,2,3 which are in database) bannerimages for 1st refresh of http://xxxx.com/filename.php and for 2nd refresh again 3(4,5,6 which are in database)bannerimages and for 3rd refresh again 3(7,8,9 which are in database)bannerimages .so please tell me total code,how it will come........

  • grahajodoh


    November 5, 2011 at 02:21 am

    thank for you tips and information..

  • Jan


    November 4, 2011 at 21:46 pm

    My sister's ex is stalking her. He is going to her business website repeatedly, now that he can't show up on her doorstep. How we can redirect his IP to another site using code she can insert into her site and perhaps even include a special personal "message" just for him? ;) I know this can be done.

  • bob


    September 26, 2011 at 10:53 am

    The real question is how to prevent morons to actually redirect you based on your IP address which is so freaking stupid I wish there was a word good enough to describe my rage

Add your comment