If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

There have been plenty of articles written and plenty of debates formed about whether or not you should cloak your affiliate links, why you should cloak them, and methods for cloaking them. While a lot of those articles give very good reasons for doing it, they don’t seem to be giving the best advice on how to do it. In this article, I am going to teach you how I cloak affiliate links.

One File Per Link

The method many people seem to be using is using one file per link. Some of them are doing it with JavaScript redirects and others are using PHP header redirects. The problem with these techniques (or any similar ones) is that you must create a new file for each link you want to cloak. After a while, you could be managing a ton of files.

Another problem with cloaking affiliate links this way is that apparently, internet users are starting to become skeptical of links ending in “.php” and will hesitate to click them.

One Folder Per Link

To hide your affiliate links even further, CashQuests has suggested creating one directory per affiliate link. Because servers will serve a default file when no file name is specified, you can create directories for your affiliate links and put an “index.php” file in each one of them. Then, you can link to “example.com/linktitle/” without anyone suspecting that it is in fact an affiliate link.

While this method does have it’s benefits, I think they are outweighed by the fact that managing one folder and one file for each affiliate link is tedious and a bit silly.

One File For Many Links

With all that persuasion out of the way, we can get into the actual technique that this article is about. I’m pretty sure this can be done (in one way or another) on any type of server, but I never use anything other than PHP and Apache, so that is what this is compatible with. There is a very good chance that the server you are working with is also running PHP and Apache.

Anyways, the first thing we need to do is create our PHP redirect file. This file is going to store definitions for each link and handle all of the redirecting. Here is an example template of what it should look like:

<?php
# get the title and define the links array
$title = $_GET['title'];
$links = array();

# ad network links
$links['azoogleads'] = 'http://www.azoogleads.com/affiliates.php?id=2039';
$links['cj'] = 'http://www.cj.com/?id=34393';
$links['reviewme'] = 'http://www.reviewme.com/refer.php?pub=10943';

# product links
$links['seobook'] = 'http://www.seobook.com/?refer=32498';
$links['adwordsguide'] = 'http://www.perrymarshall.com/affiliates.php?id=6933';

# redirection
header('Location: '.$links[$title]);
?>

Note: All of the affiliate links are fictional; I made them up for examples.

If you don’t understand what is going on there, don’t worry, it is very simple and I will explain it. Everything with a “#” symbol before it is just a comment. They are useful for leaving notes and explaining what is going on. The first line of actual programming gets the value passed in the URL as “?title=xxxxx” and stores it in a variable. The second line establishes the array variable that will store all of your link definitions.

Each of the lines starting with “$links” is defining a link redirect. The part between the brackets is the title that you will reference in your URL and the part after the equal sign is the real link that the visitor will be sent to. To add a new link, use one of those lines as a template, filling in a title “keyword” and the actual link.

The last line is the same header redirect that has been used in the other examples, but instead of hard coding the link in there, it chooses the appropriate definition based on what title was passed in the URL.

So how do you “pass” something in the URL and how do you actually use this file? Well, lets say the file is on your website as “example.com/go/index.php”. Just linking to that file will do nothing because you didn’t tell it which definition to use. If you want it to work, you link to “example.com/go/index.php?title=azoogleads”. The file will get “azoogleads” as the title variable, look up the redirect link, and perform the correct redirect.

But that doesn’t look much better than the normal affiliate links you are trying to hide, does it? Well, there is a way to make them look however you want.

Cloaked Link Styling

This is the part that needs an “Apache” server. You can use a “.htaccess” file to make your links look however you want them to. If you don’t have this file on your server, you can create it in your root directory (to set site wide options) or within the directory of your redirect script (to only affect that directory and any under it).

Inside that file, add the following:

RewriteEngine On
RewriteRule go/(.*)/$ /go/index.php?title=$1 [L]

That’s it! Now you can link to “http://www.example.com/go/azoogleads/”. The file will, once again, get “azoogleads” as the title variable, look up the redirect link, and perform the correct redirect.

You can customize this to make your links look like anything. Just edit the part before the dollar sign on that “RewriteRule” line. For example: you could change “go/(.*)/” to “recommended/(.*).html” to make you links work like “example.com/recommended/azoogleads.html”. Or, you could include multiple “RewriteRule” lines so that you have different choices.

You could also actually just put all of your link information in this file and do away with the PHP file all together if you wanted to. Here is an example of what that would look like:

RewriteEngine On
RewriteRule go/azoogleads/$ http://www.azoogleads.com/affiliates.php?id=2039 [L]
RewriteRule go/cj/$ http://www.cj.com/?id=34393 [L]
RewriteRule go/reviewme/$ http://www.reviewme.com/refer.php?pub=10943 [L]

Taking It Further

There are tons of different things you could do with a little PHP knowledge of your own. One example would be to store all of the link definitions in a database (instead of directly within the file) and create a cool management system for them.

It is highly recommended that you cloak your affiliate links. It helps to protect your commissions, increases click through rates, is better for search engine optimization, and just plain looks better. But, if you are going to do it, consider saving yourself some time and using this method rather than creating a new file for each link.

Update: Using With WordPress

It has been brought to my attention that this technique does not work with WordPress when SEO friendly URLs are turned on. Here is an easy solution I have come up with to make it work correctly.

What the WordPress “.htaccess” code does is redirect everything to “index.php” and overrides everything else (such as the rules defined in this post). To fix this (without modifying anything else), you must add your affiliate link rules above the WordPress code and tell the server to ignore all other rules once it finds one to be true. This is done by adding the “[L]” flag after each “RewriteRule” line. The examples above have already been updated to include this flag.