How to redirect outbound affiliate links

  • 0
  • 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

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.


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

About Patrick Altoft

Patrick is the Director of Strategy at Branded3 and has spent the last 11 years working on the SEO strategies of some of the UK's largest brands. Patrick’s SEO knowledge and experience is highly regarded by many, and he’s regularly invited to speak at the world’s biggest search conferences and events.

  • Tomek

    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

    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
    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

    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

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


  • Ryan

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

  • Dave

    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.



    if($id == "amazon") {
    $url = "Location:";
    elseif($id == "ebay") {
    $url = "Location:";
    else {
    $url = "";

    header("Location: $url");
    echo(" n");
    echo(" Redirectn");
    echo(" n");
    echo(" n");
    echo(" This page can now be found here: $urln");
    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

    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.

Like what you see? Talk to an Expert