There are various ways to redirect a page, but if you want to:

1. redirect conditionally based on where the visitor comes from

and

2. track the visits with Google Analytics,

…the method below works. Feel free to modify the code to your liking.

I developed this method specifically because of EzineArticles. I like to use EzineArticles.com to write articles and drive traffic to affiliate products. However, they only allow you to use a top-level domain as a URL for an affiliate product.

But I’m cheap. I don’t want to have to buy a brand new URL for every affiliate product I recommend. So I bought a generic domain, and placed this code as my index.php file.

Now, all I have to do is edit it for a keyword that I know will be in my article titles. When someone reading my article at EzineArticles.com clicks on my domain, the referer URL contains the name of my article. And since I know which article titles should point to which affiliate link, I can easily assign the correct redirect URL based on article keywords.

For example, let’s say that I have three affiliates that I’m promoting: one on Dog Training, one on Potty Training, and one on Dieting.

My code might look something like:

<?php $redirectURL = “http://www.yourdomain.com/index.htm”;

if (isset($_SERVER[”HTTP_REFERER”]))
{
if (strpos($_SERVER[”HTTP_REFERER”], “Diet”) !== false)
{
$redirectURL = “http://www.your_diet_affiliate_link.com”;
}
else if (strpos($_SERVER[”HTTP_REFERER”], “Dog”) !== false)
{
$redirectURL = “http://www.your_dog_training_affiliate_link.com”;
}
else if (strpos($_SERVER[”HTTP_REFERER”], “Potty”) !== false)
{
$redirectURL = “http://www.your_potty_training_affiliate_link.com”;
}
else if (strpos($_SERVER[”HTTP_REFERER”], “Toddler”) !== false)
{
$redirectURL = “http://www.your_potty_training_affiliate_link.com”;
}
}

?>
<html>
<head>
</head>
<body>
<!– html redirect –>
<META HTTP-EQUIV=”Refresh” CONTENT=”1; URL=<?php echo $redirectURL ?>”>
</body>
</html>

A few things about this code. First, I use HTML and meta-redirect from the body for a reason…see below for the explanation (about Google Analytics). Second, you’ll notice that I have the Potty Training affiliate link in there twice. That is because for the sake of this example, I’m assuming that you have two separate EzineArticles published…one is called “Potty Training in a Day” and the other is called “Training Your Toddler to Use the Toilet”. So you need an ‘if’ statement that will capture each article.

Avoid using common words…if you used ‘Training’, then visitors from your Dog-Training article might end up at your Potty Training affiliate.

Lastly, the “strpos” function in PHP is case-sensitive, and EzineArticles requires initial caps for article titles…so yes, you really do want to capitalize each of your keywords in your php conditions.

Now…that code works by itself. However, the reason I chose HTML Meta redirecting (vs. a Javascript window.location or a php redirect) is that I ALSO want to track inbound visits with Google Analytics!

Google Analytics code is very tricky on redirect pages. Redirection typically happens so fast that GA doesn’t get to track the page. After much trial and error, I finally learned that using HTML in this way will allow GA to track the hits. So, for example, I could see what percent of my visitors are coming from EzineArticles vs. GoArticles.

The entire page with the PHP and GA code looks like:

<?php

$redirectURL = “http://www.yourdomain.com/index.htm”;

if (isset($_SERVER[”HTTP_REFERER”]))
{
if (strpos($_SERVER[”HTTP_REFERER”], “Diet”) !== false)
{
$redirectURL = “http://www.your_diet_affiliate_link.com”;
}
else if (strpos($_SERVER[”HTTP_REFERER”], “Dog”) !== false)
{
$redirectURL = “http://www.your_dog_training_affiliate_link.com”;
}
else if (strpos($_SERVER[”HTTP_REFERER”], “Potty”) !== false)
{
$redirectURL = “http://www.your_potty_training_affiliate_link.com”;
}
else if (strpos($_SERVER[”HTTP_REFERER”], “Toddler”) !== false)
{
$redirectURL = “http://www.your_potty_training_affiliate_link.com”;
}
}

?>
<html>
<head>
</head>
<body>

<script type=”text/javascript”>
var gaJsHost = ((”https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(”%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>
<script type=”text/javascript”>
var pageTracker = _gat._getTracker(”UA-xxxxxxx-xx”);
pageTracker._initData();
pageTracker._trackPageview();
</script>
<!– html redirect –>
<META HTTP-EQUIV=”Refresh” CONTENT=”1; URL=<?php echo $redirectURL ?>”>
</body>
</html>

(remember to replace the x’s with your relevant Google Analytics ID)

So, now visitors to the site will either get redirected to a base index.htm page, or if they are coming from a site relevant to one of your affiliate keywords, they get routed directly to that link…and you get to buy only one main domain!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

1 Comment to “Conditional Redirect”


  1. Nick — April 30, 2008 @ 10:21 am

    Excellent, has taken all day to find this simple elegant solution…thank you!

    Nick



Write a comment


Name




    
Best Free Information is based on WordPress platform, RSS tech , RSS comments design by Gx3.