Cloaking Your WordPress Affiliate Links
Okay, I previously mentioned I’d talk about cloaking your affiliate links, why it is important and how to do it.
Just to be sure we all know what’s being discussed, an affiliate link is a link on your web site or blog to a sales page or process on a different web site with a different domain name.
An affiliate link might look something like this:
http://www.nastylink.com?aff=1234
That’s an ugly link and it reveals your affiliate ID to the general public. The argument goes that some potential buyers might be put off when they see it. Either they don’t like the fact they are changing to another web site or they just don’t like clicking on affiliate links and prefer to deal direct with the vendor.
Some suggest these types of links make your business look unprofessional and others recount scary stories about affiliate link hijacking and other types of link fraud that are made possible due to your affiliate details being readily available in the link.
I don’t have a business yet so I can’t say from experience if these warnings about customer attitudes or trading risks are true. But enough senior marketers make the case so I’ll go with their opinion and cloak my links.
A cloaked link is an interim link that is presented in a much friendlier and more secure manner which then redirects to the real affiliate link, meaning the real affiliate link will never be seen by people using your site.
For example, we might produce a link such as:
http://www.my-web-site.com/route/nicelink
We have that link redirect, behind the scenes, to the nasty link (or cloaked link) given in the first example. This redirection process is handled by the web server and is transparent to the user.
It probably all sounds very complicated but if you are using WordPress the process is quite simple. In fact there are plugins available that do this for you. If you don’t know what a .htaccess file is or have never heard of PHP then definitely use a plugin, don’t go messing with your web server!
I haven’t used a plugin because I want a method that allows me the greatest flexibility in the future and because the task really isn’t that difficult if you have a little experience. So I’ve decided to roll my own.
The process involves creating a simple PHP script and pasting a single line into your .htaccess file. The .htaccess file contains configuration and file handling instructions for the web server on which your site is running. If you are going to use this script then make sure you follow along precisely as any errors in this file will likely cause your web server to fall over. So make a copy of your .htaccess file first so you can revert to it if things go wrong. Honestly – make a copy!
But first the PHP script. Open up wordpad or notepad and copy/ paste the following:
<?php
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$id = trim(strtolower($id));
$url = false;
switch ($id) {
case 'nicelink': // Example label
$url = 'http://www.nastylink.com?aff=1234'; // The real affiliate link
break;
}
if ($url) {
header('Location: ' . $url);
exit;
} else {
// Can't find the affiliate ID so either exit or maybe put some
// code here to redirect to an error page or another sales opportunity
exit;
}
}
?>
Now decide on a label for the affiliate link you want to cloak, it can be anything provided you haven’t already used it for another affiliate link. Don’t use spaces or any other special characters in your labels, stick to letters, numbers and underscores or dashes. In the example we choose the label nicelink but you should pick something more meaningful that helps you quickly identify your affiliate links. For example, you might use hostgator for your Hostgator affiliate link. Go ahead and edit the script replacing the example label with your own. Now replace the example affiliate link with the affiliate link you want to cloak.
These are the only two parts (marked in red text) that you need to edit, leave everything else unchanged. Save this file as mp-route.php to the folder in which WordPress is installed. You can change the file name if you like but that means you will also have to reflect the change in your .htaccess file, so for simplicity I advise sticking with the suggested file name.
In the same folder you should find your .htaccess file. Once you have backed it up, open it and you should see something like this (for a basic WordPress 2.9 installation).
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
There may be other information in the file but it’s the BEGIN WordPress… END WordPress section you are looking for.
Insert the line highlighted in red below, directly after RewriteBase / so you end up with the following.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^route/(.*)$ mp-route.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Immediately go and test your home page in a browser to ensure your site is still operating correctly. If not, restore your .htaccess file from the backup you made earlier and retrace your steps to ensure you followed all procedures precisely.
If all is well you should now be able to use your new cloaked links. In the example, use http://www.my-web-site.com/route/nicelink in your posts and the web server will automatically redirect it to http://www.nastylink.com?aff=1234 without the user being any the wiser. Do you see the label nicelink at the end of the friendly URL? Don’t add a trailing slash, by the way.
Your friendly links must always start http://www.my-web-site.com/route/ and then you just add the label you have chosen, which gives us http://www.my-web-site.com/route/nicelink. Obviously change the www.my-web-site.com part to your real domain name.
That takes care of one link, but what if you need more? To add another cloaked link go and edit the PHP file you created, like so.
<?php
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$id = trim(strtolower($id));
$url = false;
switch ($id) {
case 'nicelink': // Example label
$url = 'http://www.nastylink.com?aff=1234'; // The real affiliate link
break;
case 'nicelink2':
$url = 'http://www.anotheraffiliate.com?aff=5678';
break;
}
if ($url) {
header('Location: ' . $url);
exit;
} else {
// Can't find the affiliate ID so either exit or maybe put some
// code here to redirect to an error page or another sales opportunity
exit;
}
}
?>
The blue section of the example script is just a copy/ paste of the preceding section but with a new label and affiliate link. Copy and paste again for as many links as required.
If you have a whole bunch of links you could put them in a database and look them up from this script, or you could add tracking code or do pretty much anything you want with the links before sending traffic to the affiliate. But that’s for advanced users only.
If this works for you or you have any problems or spot a flaw, drop me some feedback.
SearchPlacementPros.com SearchPlacementPros.com will use the latest search engine optimization strategies to get your site to the top.

Better to use subdomains so like affiliate.mydomain.com so you can track in google.
Hi there Saipan and thanks for being the first person to comment on this blog! Sure, setting up sub-domains for your affiliate links is another way to go and I know a lot of top marketers use that method. It’s all about personal choice, I prefer something that’s dead easy to maintain and simple to understand and explain to others. I’m not sure I follow you about Google though, do you want you affiliate links to be indexed? Is there some advantage to that? If so, it’d be great if you could follow-up with a quick explanation. Regards.
Twitter: NetSparx
says:
Great tip! I will be using this code in my WordPress blogs.
I think the subdomains are ok but they have drawbacks:
1. They take up too much time to implement
2. They are not as portable as the code above
3. You know from just looking at one file what your Affialite IDs are.
Thanks!
.-= Fred´s last blog ..New Revenue Sharing Adsense =-.
Hi Fred, thanks for your comment. I’m delighted you find the script useful. You probably already realise it’s really just a starting point to do many other things so if you modify or extend it and are feeling in a generous mood, please let us know.
Came across your blog post when i was doing some research for school (got distracted). I am very new to internet marketing but am starting to look at promoting clickbank. Do affiliate sites care if yo cloak their links or does it matter to them at all. I just want to make sure it is wrong.
Hi Lanie and thanks for taking the time to comment. As far as I know, the affiliates are fine with cloaked links and even recommend or demand them for email campaigns. Making sure unscrupulous buyers don’t steal your commission is a perfectly legitimate reason for using cloaked links, as far as I’m concerned. I’m not so sure about trying to mislead the buyers by making them think they are still on your site. However the new disclosure legislation takes care of that.