Electrosmart Digital Publishing BMAPS Digital Publishing
Members Vault
ESL Login

Latest Updates

IM News
Free Tools


5 Steps to Being a Successful Author

Guidelines for Editing Non-Fiction Books

Book Formatting

Selecting Font Size and Type

Getting Published

Realistic Odds of Getting Published

7 Steps to Writing A Book Publishing Query

Tips on Writing Book Proposals The Way Editors Want

Publishing Ettiquette

website security

Using PHP Redirects


How to mask an affiliate link using a server side scripting language.

There are three parts to putting masking redirects in place:

  1. The redirect page – call it whatever you want. For this example we will call it ‘redirect.php
  2. The robots.txt file - used to restrict areas of your websites that are considered private or have some kind of security sensitivities, or, are out of date.
  3. Affiliate links – these will link to redirect.php


Create The Redirect Page

Create a subdirectory called ‘redirects’, ‘products’, ‘link’ or ‘recommends’ – you can call it whatever you like.

Create a file in that subdirectory to contain your outward scripts. You can either use a single file or you can split them to help organize your product groups. For the purpose of this example, I will use:



Redirect Page Script

Create a new file in your HTML editor - save it as 'redirect.php'.

If you are using Dreamweaver - open the source view and strip out the HTML code that DMX adds automatically - you only need a clean page.

Add the following lines of code to the php page:

$linkid = $_GET['linkid’];
if ($linkid == "") {$link = "http://www.yoursite.com";}
elseif ($linkid == "1") {$link = "http://www.merchant1.com/";}
elseif ($linkid == "2") {$link = "http://www.merchant2.com/";}
header("Location: $link");

What the code means is:

$linkid – refers to the links we will use from the pages with affiliate links on. 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 next two lines 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.

Upload the file to the new directory on a php supporting web server.


The Robots.txt File

You now want to block that subdirectory in the robots.txt file, using the following script:

Disallow: /recommends/

Or you can just block the file [if you have other files in that subdirectory you want indexed], using the following script

Disallow: /redirect.php

This will prevent any search engine spider from following links on your webpages to that directory, and knowing what is the final destination URL of the link on your web page.

If you want to be doubly sure, put both:

Disallow: /recommends/
Disallow: redirect.php

NOTE: This assumes that your robots.txt file uses

User-agent: * [which covers all the bots], otherwise you will need to add specific lines for each bot, for example:

User-agent: googlebot
Disallow: /recommends/
Disallow: redirect.php
User-agent: msnbot
Disallow: /recommends/
Disallow: redirect.php
User-agent: slurp
Disallow: /recommends/
Disallow: redirect.php

The ‘disallow’ line tells the robot that it cannot crawl that file. In this case , we have put redirect.php file in the root of the server, but you can add it anywhere. Save and upload your completed robots.txt file to the root of your server.


The Affiliate Links

Now, instead of using the affiliate URL link on your page, use links in the format:
http://www.yourdomain.com/recommends/redirect.php?id=[x] where 'x' identifies the affiliate link.

You now need to enter the redirect ID into the product.html file, for instance

Your product.php script should then do the mapping from 'id' to affiliate URL and then perform a 301 redirect to this URL.


Video Demonstration

For a video demonstration on how you can do this [14mins]


Website Tools Index | Favicon | Code Protection | PHP Redirects | Google Analytics | CP SiteSaver