Redirecting based on referrers.

Status
Not open for further replies.

dschus

New member
Feb 2, 2008
528
8
0
South Dakota
I have a particular wordpress post that is really getting me a lot of natural traffic from google and a social media site. In order to bank on this I was thinking that by redirecting my users to a landing page instead of the wordpress post, I could easily get some conversions. What would be the best way to do so? Keep in mind I use wordpress and am not sure where I would place this code?

Any help is appreciated.
 


Put something like this in your header.php file:

$ref = _SERVER['HTTP_REFERER'];
$redirectRef = 'http://www.digg.com';
$landingPageUrl = 'http://www.landingpage.com';

if ($ref == $redirectRef){
header('Location: ' . $landingPageUrl);
}
 
Probably something like this:


PHP:
<?

if ($_SERVER['HTTP_REFERER'] == 'whatever.com'){

header("Location: http://www.YourURL.tld");

}


?>
This needs to be before the starting HTML tag I believe.

If you are using multiple referrers you may want to use something like a switch case statement. If it were me I would just write a big if then else else else else statement. There is probably a more efficient way though.

Actualluy you probably would want to do some kind of break so that you could grab just the domain name and you wouldn't have to match airline tickets cell phone deals at whatever.com and/or whatever.com and/or whatever.net , etc
 
Much more practical example that allows you to have multiple referers and redirects.

PHP:
<?
//$referer holds an array of the referers you want to check against
$referer = array("google.com","yahoo.com","msn.com");

//$redirect is a corresponding array of redirect links
$redirect = array("www.site1.com", "www.site2.com", "www.site3.com");

 for($i=0;$i<count($referer);$i++)
      {
          //check to see if a referer we are looking for is contained in our actuall referer
     if(stristr($_SERVER['HTTP_REFERER'], $referrer[$i]))
        {
            //if it is then lets redirect them
        header("Location: " . $redirect[$i]);
        }
  }

?>
Enjoy!
 
  • Like
Reactions: WildGunner
You could also try something like this:


Testing Referrer


PHP:
<?php


$url = parse_url($_SERVER['HTTP_REFERER']);

$domain = $url['host'];


if ($domain == 'www.wickedfire.com' or 'wickedfire.com'){
    header("Location: http://www.google.com");
}
elseif ($domain == 'yahoo.com' or 'www.yahoo.com'){
    header("Location: other location");
}



?>
 
You could also try something like this:


Testing Referrer


PHP:
<?php


$url = parse_url($_SERVER['HTTP_REFERER']);

$domain = $url['host'];


if ($domain == 'www.wickedfire.com' or 'wickedfire.com'){
    header("Location: http://www.google.com");
}
elseif ($domain == 'yahoo.com' or 'www.yahoo.com'){
    header("Location: other location");
}



?>

Look at comments in code for response.
PHP:
<?php


$url = parse_url($_SERVER['HTTP_REFERER']);

$domain = $url['host'];

//Your better off with a string comparison then you could just use 'wickedfire' or 'wickedfire.com'
if ($domain == 'www.wickedfire.com' or 'wickedfire.com'){
    header("Location: http://www.google.com");
}
//this elseif not needed as there is no redirect if its not from a referer he wants to rederect from
elseif ($domain == 'yahoo.com' or 'www.yahoo.com'){
    header("Location: other location");
}



?>
 
rage9. I'm still a php noob. Thanks for pointing out stristr. I think your code is a better way.

So to understand this:

for($i=0;$i<count($referer);$i++)

If I is less than the number of items stored in the $referer it runs the loop. This is needed because your matching the sites in an array correct?
 
Yes you got it. $i is needed because we have created two arrays:

Code:
[COLOR=#000000][COLOR=#0000bb][COLOR=White]$referer = array("google.com","yahoo.com","msn.com");

$redirect = array("www.site1.com", "www.site2.com", "www.site3.com");[/COLOR][/COLOR][/COLOR]
$i is a basic counter. So the array items are set up like this:

Code:
$referer[0] = google.com
$referer[1] = yahoo.com
$referer[2] = msn.com

$redirect[0] = www.site1.com
$redirect[1] = www.site2.com
$redirect[2] = www.site3.com
This way we can easily use one variable ($i) to access both arrays. So here's a little walk through:

Code:
 for($i=0;$i<count($referer);$i++)
Set up a counter, $i will have a starting value of 0, $i++ means the variable $i will be incremented by 1 each time through the loop, and the loop ends when $i equals more that how many items are in our array represented by count($referer). In this example count($referer) is equal to 3.

Code:
if(stristr($_SERVER['HTTP_REFERER'], $referer[$i]))
We make a check to see if our referer string is inside the HTTP_REFERER. On the first pass $referer[$i] equates to $referer[0] which has a value of google.com.

stristr
will return FALSE if there is no match, so we only test against a non-False clause.

Code:
header("Location: " . $redirect[$i]);
For the sake of argument, lets say the previous expression is true, $redirect[$i] will evaluate to $redirect[0] which has a value of www.site1.com. This will end up making the line look like:

header("Location: www.site1.com");

And the user goes on there marry way. Now if we didn't get a match, the whole possess starts over only $i will be incremented by 1, to a value of 1.

Any more questions feel free to ask.
 
  • Like
Reactions: spyderman4g63
Status
Not open for further replies.