PHP Redirect For Internet Marketers

Posted on December 11th, 2009 by admin in articles

Have you encountered this problem?

You own a site that pushes an affiliate product. There are affiliate links on your site pointing to the sales page on the merchant's site. Other sites link to your site with a "buy it now" message. The problem is, when they click that "buy it now" link to your site, they have already made their buying decision and have committed themselves to it (much of the time anyway). To then land on my page which tries to sell the product to them again, just doesn't make sense.

Ideally, you want to display your "sales pitch" page to visitors arriving from search engines but for visitors arriving via "buy it now" links to your site, you want to redirect the user immediately to the merchant's site,  not forgetting to send your affiliate tracking code with them.

I encountered this problem recently when I bought an expired domain that was perfect for pushing a digital download. I discovered that many visitors were arriving from one site in particular that had a link to mine saying something like "Buy XXX for $39.95". Visitors clicking on this link have already made their buying decision, but they were then presented with my sales page. They then had to read my sales message, decide that they couldn't buy the product on mys site and then click again to reach the merchant's site where they could make their purchase. In this case, my site was an unecessary obstacle. I would imagine that making potential buyers jump through hoops in this way is not good for business.

PHP Redirect

I'm no technical whizzkid, but I found an easy way to use PHP to incorporate the following logic:

  1. Visitors via "buy it now links":  redirect to merchant's site
  2. Visitors from search engines: display my sales page to seduce them

I'm lucky because in my case, all the "buy it now" links to my site are from one site only. Also, at the moment my site is just a one page html site, meaning that I have to put the PHP code on only one page.

There are two tasks that we need to perform:

  1. determine where our visitors came from. This is the referrer, which is found in the $_SERVER['HTTP_REFERER'] variable.
  2. Redirect to our the merchant's site using our affiliate link. We can do this using:
    header( 'Location: http://www.merchant.com/click-123' )

This is the code I used in its entirety. In its entirety - ha! It's just a couple of lines. The code goes right at the top of the page, before any other code. That means even before the doctype declaration.

<?php

$ref = $_SERVER['HTTP_REFERER'];
$omitUrl = array('buy-it-now-linker.com');
foreach ($omitUrl as $source) {
if (strpos($ref,$source)!==false) header( 'Location: http://www.merchant.com/click-123' );
}

?>

The $omitUrl array holds all the referrers we want to redirect. Note that I've made it an array in case I find other referrers we need to redirect. In that case we can simply add another element to the array. The code takes the referrer and checks whether it includes "buy-it-now-linker.com" (the linking site with the "buy it now" link). If so, the visitor is redirected to the affiliate link. If the referrer isn't "buy-it-now-linker.com", the redirect is ignored and the rest of the page is rendered.

Note that the page I added it to was index.html. Leaving the .html extension as it is means that the PHP code doesn't get executed. Therefore, we need to change the extension to .php.

The Impact Of Adding The Redirect

I'm using Google Analytics to track my traffic, and the GA code goes at the bottom of the page. The PHP redirect goes right at the start. This means that whenever visitors arrive from the "buy it now" link, they get redirected immediately and the GA code never gets executed. Thus, this type of traffic never gets tracked. Of course, I didn't realise this and freaked out when my traffic suddenly plummeted from around 200 visitors / day to 10!

Fortunately, I can track that traffic using the stats that Commission Junction provide, as they handle the affiliate relationship for the product I'm pushing.

Leave a Reply

More News