This is a simple script for setting up redirects to affiliate merchant links without using a database. It can be added to any site that can run PHP code.
If you are not familiar with click scripts or jump scripts, they are used to block affiliate links from search engine spiders. While using a click script is not absolutely necessary, it can be good idea because a properly configured script will not bleed off PageRank or create problems for your merchants due to the use of hundreds of variations of URLs with different affiliate codes that can all be pointed at the same page in a merchant’s site. It can also be a good method for concealing your affiliate IDs from competitors. We are using a method recommended by Google to help prevent spiders from following your affiliate links.
You can call this PHP script anything that you wish. For simplicity, we are calling it click.php. The first thing you need to do is to add a Disallow statement to the robots.txt file in your site. This will tell spiders to disregard all links pointed to click.php.
If you already have a robots.txt file, simply add the Disallow statement to the “User-agent: *” statement. If you do not have a robots.txt file, create one with a pure text editor, such as Microsoft’s Notepad, and add the following.
User-agent: * Disallow: /click.php
Do not use Word or another word processor for creating or editing the robots.txt file. You must use a pure text editor. Save the file and transfer it to your site’s root directory.
Next, cut-and-paste the following code into a PHP file that uses the file name you have decided to use. Substitute your affiliate links for the value side of the $affLink name-value pair. Use the HTML version of the affiliate links provided by your affiliate network, but just use the actual HTTP address and not the entire hyperlink structure that is provided.
You are creating a simple numeric array that will be called by its numeric key. The array can contain as many entries as you wish. You do not have to escape any code or do anything special to the affiliate links. The array can contain multiple variations of affiliate links from several affiliate networks.
<?php $aff = isset($_GET['aff']) ? $_GET['aff'] : 0; $affLink[1] = 'http://rover.ebay.com/rover/1/711-1751-2978-328/1?aid=10369560&pid=yourid'; $affLink[2] = 'http://clickserve.cc-dt.com/link/tplclick?lid=41000000027367964&pubid=yourid'; $affLink[3] = 'http://www.tkqlhce.com/click-yourid-10363726'; $affLink[4] = 'http://www.amazon.com/exec/obidos/ASIN/B000UPOJ5W/ref=nosim/yourid-20'; if(isset($affLink[$aff])) { header("Location: $affLink[$aff]"); exit; } else { header("Location: http://".$_SERVER['HTTP_HOST']."/"); exit; } ?>
Here is what is happening in this script.
Line 2 uses a ternary operation to set the $aff variable to the value passed in the aff name-value pair from a hyperlink. If there isn’t any value, it is set to a default of zero.
Lines 3 through 6 set the values for the numeric array using affiliate links.
Line 8 checks to see if a valid value was passed to $aff. If it was, the redirect in line 10 is executed using the array element selected using the value in $aff. If no value has been passed (a value of zero) or the value is invalid, line 15 redirects the user to the home page.
It is important to have a default redirection to the home page just in case you make a mistake with with a name-value pair in a link, or if a hacker decides to start playing with your links to see if he or she can break your script.
After setting up the PHP script, save it and transfer it to the root directory of the web site.
To set up affiliate links on your web pages, use the following hyperlink structure. Substitute the corresponding numeric key for the value of x. In other words, if you want the link to redirect to the Amazon affiliate link in the sample code above, then use aff=4 for the name-value pair in the query string portion of the link.
<a href="/click.php?aff=x" rel="nofollow" target="_blank">text</a>
Notice that the anchor tag contains the nofollow attribute. That basically does the same thing as the Disallow statement in the robots.txt file, but because search engine spiders only occasionally check the robots.txt file, the nofollow attribute reinforces the fact that you want search engines to disregard this hyperlink.
This is a simple solution for a web site that only contains a maximum of a few hundred affiliate links. If your site will contain more links than that, it is time for a database solution to manage your links.
Luz says
You are AWESOME! I just visited several different sites to find out how to do this and none of them worked. I FINALLY GOT GOOD RESULTS thanks to you clear instructions and explanations. THANK YOU SO MUCH!