Redirect Script for Affiliate Sites in PHP
Lets Start with a Bang!
Ok so to kick off the new blog im going to post about a technical solution to a problem that was raised recently on the A4U forum. The subject raised was how to implement affiliate link masking using a server side scripting language. Now as im fairly fluent in PHP and have also made said scripts it seems a great idea to share them. You need to be able to code a little in PHP to use this and also able to upload to your server (which im assuming most readers should be able to do!).
The Elements
There are three elements we need to get the masking going:
- The robots.txt file - a simple txt file than is specifically targeted towards controlling the behaviour of search engine robots. Its proper purpose is to restrict some areas of websites that are considered private or have some kind of security sensitivities, or, are out of date.
- The redirect page. In this case we we call our page redirect.php - easy.
- The new affiliate links. These will be the links to the redirect page that you will need to write into your pages when everything is in place.
The Robots.txt File
Open up a text editor of your choice (notepad for purists ;)) and the following code is all you will need:
User-agent: googlebot
Disallow: redirect.php
User-agent: msnbot
Disallow: redirect.php
User-agent: slurp
Disallow: redirect.php
The ‘User-agent’ refers to the robot of a certain search engine. So googlebot for Google and msnbot for MSN etc. Different search engines have different bot names so if you want to block others aside from the big three you’ll need to find out their descriptions before you go ahead and add lines to your robots.txt file. The ‘disallow’ line tells the robot that it cannot crawl that file. In this case the redirect.php file will be in the root of the server to make it easier. Save and upload your completed robots.txt file to the root of your server.
The Redirect Page
Open up your favourite text editor or website creator and begin a new PHP file. You’ll need to add the following lines of code:
<?
$linkid = $_GET['linkid'];
if ($linkid == "") {$link = "http://www.yoursite.com";}
if ($linkid == "1") {$link = "http://www.merchant1.com/";}
if ($linkid == "2") {$link = "http://www.merchant2.com/";}
header("Location: $link");
exit();
?>
Lets look at the code. The $linkid variable is provided in the links we will use from the pages with affiliate links on - more on that in a moment. Each link is assigned a number and placed into a conditional statement. The statement compares the $linkid value and assigns the $link variable the correct url. The final two lines then forward the browser to the $link location url using the header function in php. Add as many links as you need, most probably every time you add a new link to your site that needs masking. Save this file as redirect.php and upload to the root of your PHP enabled server. We now need one more essential element….
The New Affiliate Links
Previously your links may have looked like this for all to see:
http://scripts.affiliatefuture.com/AFClick.asp?affiliateID=xxxxx&merchantID=xxxx&programmeID=xxxx&mediaID=x
Take the raw affiliate links and place them into your redirect.php file. Assign them numbers as described above. Lets say for now the affiliate future link above is named as ‘1′. Now you are ready to write your new link just like this:
<a href="http://www.yoursite.com/redirect.php?linkid=1">Affiliate Future Merchant Name</a>
And voila! The new link should now go through the redirect page and the search engines (well those that obey the robots.txt file) won’t see it. If you are really paranoid about search engines following afiliate links try adding rel=”noindex, nofollow” to your masked links.
There are many other things you can do with a redirect script to help with analytics and monitoring of your websites, one example is to add popular links (or products) to a database on the fly. Another is to send an email to yourself if a link has been clicked. Ill cover that in another post soon!

Just as a footnote to this post, if you want to see a redirect in action just look at the ‘Affiliate Networks’ on the right hand side. They use this technique.
Brilliant post, very helpful and easy to understand for non techs such as myself - I now have redirects working on my site, thanks Dave!
[…] look better with a redirect in place anyway, so I for one, always do it. If i may be so bold - heres how to make a redirect for affiliate links Dave __________________ TRAVELPIXEL.NET Get your travel site listed on the site with the small […]
Thanks for this script, just what I’ve been searching for. I was struggling to see why it wasn’t working though - there’s a ” ‘ ” missing after linkid on Line 2.
Thanks again!
Thanks for pointing that out Phil, should be fixed now.
as pointed out kindly on the A4U forums by Diobach, you can cover all the user agents in your robots.txt file with the following code:
User-agent: *
Disallow: redirect.php
This will cover all SE robots and any other robots that may try to access the file.
Hi David,
Is it possible to use this script to count the number of times an individual link is clicked? SO I could look every few days and see what the most popular link was by number of clicks?
many thanks,
Marc
[…] Re: Is there an easy way to mask aff links? AFFILIATE CITIZEN
Hi, i just wonder if this suggestion could also be used for in-page redirects??
I mean like this:
Someone fills a form, then they come to a Thank You page.
The thank you page would have a few Thank You messages as “if” statements to display, depending on the form input, or also have a redirect to another page, also depening on the input and the corresponding if statement.
Could it be done and work??
Example
I want to redirect to another page only if these 2 statement where activated by the form input
{if site_new==1}
{if ispayment==1}
How would i have to rewrite your scriptlet to do exactly that - while doing no redirection if any of the other if statements on the Thank You page where selected??
Probably not so, but how then??