How to redirect outbound affiliate links

by Patrick Altoft on August 18, 2007

Most of the top affiliate marketing experts redirect outgoing links. Some use a tracking script to track click volume but the main benefit is that your visitors are not immediately aware that the link is an affiliate link and the search engines don’t see that your site is just an affiliate site.

There are loads of affiliate link management scripts on the market but I’ve never seen one that was simple so I decided to release my own ultra simple version.

This script will create links in the format www.yoursite.com/go/amazon.php

Add the following code to your .htaccess file:


RewriteEngine on
RewriteRule ^go/(.*).php$ go.php?id=$1 [L]

Download this file and change the file extension from .txt to .php

Edit the links in the go.php file to include your affiliate links, adding more as appropriate. Upload go.php to your web server.

Cloaking

This script includes some code to identify search engine spiders and redirect them to your homepage rather than an affiliate link.

Cloaking content for search engines is against their guidelines so you should use this at your own risk.

If you don’t want to cloak for the search engines use this version of the code instead.

Patrick Altoft is Director of Search at Leeds based digital & SEO agency Branded3. Patrick also runs Blogstorm.

You can get our blog posts delivered for free by email every day - simply add your email address to the box below or alternatively grab the RSS feed.

Read some similar posts

{ 7 comments… read them below or add one }

Tomek 14 Sep 2007 at 6:37 pm

I think it is not neccesary to use cloaking or to be worry of spiders. I will just add Disallow:amazon.php or Disallow:go.php to robots.txt I think this will stop the spiders so they will have no idea about my affilities redirects..

david 27 Sep 2007 at 12:20 am

I’m new to this, but how exactly does this work? I have an amazon affiliate code and would like all references to Amazon to be sent there, can this script do it? If so how would I edit it to do so? I’m using Pligg with a mysql and php backend.

I’m also confused by the
$id=$_GET["id"];
piece of code.

Where does it get the ID from? Do I have to enter it manually?

Sorry for being so new and not understanding. Thank you in advance.

Dave 26 Jan 2008 at 10:23 am

The $id=$_GET[”id”]; bit of code means that the PHP variable $id will be set to the value of “id” from the URL. So, if the URL was go.php?id=amazon, then in PHP, $id will be “amazon”.

After this, you can add the rewrite rule so that /go/amazon will turn into /go.php?id=amazon.

You will still need to edit the go.php file every time you have a new affiliate link.

I wrote an external link redirector for a company I was contracting for a while ago. Their entire revenue stream was from affiliate links so they wanted the ability to quickly and easily edit every instance of an affiliate link across their entire website.
Sometimes, the same product can be advertised through several different affiliates and sometimes you may want to switch which one you use. Affiliates can run out of money for this campaign (we sent a LOT of traffic) and new affiliates can start a campaign for the same product and offer a better deal. By looking the product code up in a database I was able to change every link on the site and even the links we had sent out in the weekly email to point to the new affiliate.
We could also track exactly how many leads we had sent them and display it on a graph (very pretty, the CEO loved it.) This also allowed instant feedback rather than the daily or weekly feedback we got from the affiliates.

I’m a security auditor now and I just wanted to add that you have done the right thing in not just redirecting straight to the value passed in the URL. Open redirects can facilitate phishing attempts and occasionally can even be susceptible to XSS attacks.

Ray 10 Jul 2008 at 5:47 am

go.txt is not available. Please repost the link.

Thanks

Ryan 24 Nov 2008 at 5:16 pm

Your links are dead… is there anyway that you can repost this for us?

Dave 13 Mar 2009 at 1:01 pm

I can’t remember what the original PHP code looked like but it is simple enough for a mildly competent PHP coder to mock up in a few minutes.


<?php

$id=$_GET[”id”];

if($id == "amazon") {
$url = "Location: http://myaffiliate.com/redirect?thesite=54321&myid=12345";
}
elseif($id == "ebay") {
$url = "Location: http://myaffiliate.com/redirect?thesite=67890&myid=12345";
}
else {
$url = "http://mysite.com/error.html?error=REDIRECT_NOT_FOUND";
}

header("Location: $url");
echo("\n");
echo(" \n");
echo(" Redirect\n");
echo(" \n");
echo(" \n");
echo(" This page can now be found here: $url\n");
echo(" \n");
echo("\n");
?>

Obviously, you need to change the redirect URLs to the actual URLs that your affiliate has given you and change the error page at the end to be an actual error page you have created.

Whenever you get a new affiliate link, just add a new elseif() block to that file.

If you have a database, you could also log each of the clicks in the database just before you do the redirect (the header() function) and you could even look up the redirect address in the database as well. Just remember to be careful using user-supplied data in SQL queries. You can’t trust those darn users.

Dave 13 Mar 2009 at 1:05 pm

Hmmm… the blog did funny things with the quotes and stripped all the HTML tags out of the echo() statements at the end.

If you copy-and-paste this code you will need to make all the smart-quotes into normal (dumb?) quotes and wrap a basic HTML page around the message at the end for users who don’t follow redirects.

Leave a Comment (registration is optional)

Registration is free, takes about 5 seconds and is worth doing.

You can use these HTML tags and attributes:
<a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>