How To Hide All Your Affiliate Links With Only One File
Affiliate Marketing, Programming, Web Development September 9th, 2007There 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.

September 9th, 2007 at 11:54 pm
Why not just use a Free Link Cloaker? What you describe up there was a little too much for
September 10th, 2007 at 12:03 am
It really isn’t that much work… it could be up and running much faster and easier than the installation of the script your recommend. The explanation is just long for people who don’t understand what it all means.
That does, however, look like a cool script that does the exact same thing I described here and works almost exactly the same as the script I made for myself.
I guess it’s just a matter of preference which way you want to do it. That script is easy to manage and tracks clicks, but the method in this post is also easy and requires no installation and help you learn how everything is working.
In any case, thanks for the recommendation!
September 10th, 2007 at 1:24 am
Great post! I wrote literally the exact same script for my own use but you beat me to posting about it. :p
September 10th, 2007 at 4:45 am
Interesting – definitely going to try it. Thanks for sharing and the detailed explanations, Derek!
September 10th, 2007 at 12:29 pm
Derek,
Sorry I’m a little green when it comes to anything greater than HTML. Can you explain a little more for us rookies?
What do you name the php document?
Where do you file the php document or should it be put into a file folder?
For the .htaccess, I assume this is being filed on your sales page domain root directory where your using the affiliate link(s)?
What is the “directory of your redirect scripts” and where would that be filed?
I apologize for my coding/site programming deficiencies, bit I would really like to learn and use this strategy.
Chaz
September 10th, 2007 at 12:47 pm
Hey Chaz,
You can name the PHP file anything you want and put it anywhere you want. Just make sure that the file name and location matches in your .htaccess file. In this example, I saved the file as “/go/index.php”
As for the .htaccess, it basically is an options files that affects its current directory and any directories below it. Just put it in your site’s root directory to make everything easier.
September 10th, 2007 at 3:29 pm
Derek,
Thank you. I think i got it.
As you mentioned though, wouldn’t be easier if you just included all the link info in the .htaccess and don’t even use the .php?
That’s all you need to do is put go/ and your afilliate link? It will pick up any of the afilliate links on your site and redirect them?
September 10th, 2007 at 6:42 pm
[...] Derek Beau: It is highly recommended that you cloak your affiliate links. It helps to protect your [...]
September 12th, 2007 at 8:58 am
[...] A few days after Kumiko’s post, Derek Beau made a post titled “How To Hide All Your Affiliate Links With Only One File.” [...]
September 13th, 2007 at 4:23 pm
Thanks, nice way to apply a known concept to an unknown use
September 13th, 2007 at 7:17 pm
Love this post. Thanks for the info. I’ll try to aplly it.
September 14th, 2007 at 11:10 am
[...] you’re into affiliate marketing, this post about “how to hide all your affiliate links with only one file” might be something for you. It’s written by Derek, and since it is just one of many [...]
September 18th, 2007 at 10:23 am
[...] MySQL database to track clicks, use a custom link cloaking program to handle everything for you, or code a custom PHP script for all your affiliate links. Your options as an affiliate are [...]
September 18th, 2007 at 7:04 pm
If you want an even easier way to manage your affiliate links without having to write or modify any php code or .htaccess rules you should try my new plugin for wordpress that let’s you manage such links from
the wordpress admin.
you can find out more about this free plugin here:
http://patchlog.com/wordpress/hidden-affiliate-links/
November 13th, 2007 at 2:39 pm
This looks great. How can I use google analytics to track my pages?
December 31st, 2007 at 11:50 am
Very easy and straightforward, thanks!
February 16th, 2008 at 5:08 am
February 25th, 2008 at 2:17 am
My little suggestion: http://cutelink.info . I hope that my suggestion help you to completly hide your affiliate links on your web pages.
Thanks
March 4th, 2008 at 4:54 pm
Thank You Derek!
I was searching around for some useful code like this and all of the other sites suggestions didn’t work as they described (probably because of me). However, your solution works great. I was getting really tired of making a new page for every product recommendation. You just gave me more time to play. Yes!
March 6th, 2008 at 12:09 pm
This is really awesome man.Thanks for the post.Really needed it.I don’t know much about php but I could just scrape through with your advice.I was thinking can we just mask those affiliate links in the big banners which we get to advertise the products?I don’t know much about coding ,I hope you have understood my point,though it might be very sill.
April 25th, 2008 at 3:52 pm
hey mate, the post is a really cool post. i’d like u to help me with something. Actually its a MMORPG game. since the links are visible ppl are able to do SQL injection to get stuffs in the game. so if i can hide the file name i think it will be great. i need bit of ur personal help. if u can pls mail me in ma mail or add me in msn. my msn is asinan007@hotmail.com.
Great Post…. Two Thumbs Up
June 1st, 2008 at 5:39 am
Hi Derek. I followed the instructions above. I can get the link work with up to: example.com/go/index.php?title=azoogleads
I can never get it to work after following the htaccess stuff. I think i’m putting the htaccess in the wrong place. I have the following already in my htaccess:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Where do I add the code: before, in between or at the top. Thank you so much.
Robert
June 30th, 2008 at 6:15 pm
Thanks, been looking for a script like this forever, promoting a few hundreds affiliate links can take forever if you are using a link cloaker, but this script makes it so much easier, thanks a ton.
June 30th, 2008 at 6:34 pm
Just wanted to say thanks again before I close the page, I am in so relieved that I no longer have to resort to off-site link cloaking or using aa page per link and I can manage all my affiliate links from a single directory… You rock!!
August 12th, 2008 at 5:07 pm
This is awesome – it helps me out a WHOLE lot.
August 14th, 2008 at 2:17 am
Lovely post. Please add my email address to your list and email me the updates if possible. I always like to read your blog and comment on it.
October 10th, 2008 at 8:56 pm
I’m trying a lot of plugins to cloack my affiliate plugins, none of them works like I want, and somehow I end up in this post,.. I tried and work like a magic. Thank youuuuu so much for the lovely post. Very helping,..
October 27th, 2008 at 2:43 am
Services that I know being used by some affiliate marketers:
1. TinyURL.com
2. URLFreeze.com
3. TwistUrl.com
4. redirectmagic.com
5. shorturl.com
6. yournewurl.com
7. hideyouraffiliate.com
They work by replacing the long affiliate link with a short tinyurl.com/12345, or urlfreeze.com/12345 accordingly.
Whilst efficient at first glance, you have to keep in mind the following:
• By going to their website it is possible to trace the url and expose the affiliate link, hence defying the purpose of hiding. People who are aware of this are usually those who have been involved in Internet Marketing at some point.
• To the novice, those links can be a potential deterrent, since it has nothing to do with the provided product. And of course, it doesn’t serve your branding goals.
So, when using this we are scaring off the novice, and aren’t doing anything to protect against the savvy. Therefore I wouldn’t recommend using those.
You can also check out the following :
http://www.addme.com/linkcloaker.htm
Vineet Aggarwal
http://www.ILoveExtraCash.com
May 12th, 2009 at 11:01 am
[...] read all these ways that can be found from google: How To Hide Affiliate Links | John Chow dot Com How To Hide All Your Affiliate Links With Only One File DerekBeau.com Ugly Affiliate Link: I Don
June 17th, 2009 at 1:01 pm
Whoa, that’s a lot of effort.
I was looking for something nice and simple the other day and found Tim Robinsons Affiliate Link Cloaker. It took literally 5 seconds to set up and does everything you listed.
Soo much better for newbies like me who run away the mere site of code.
October 24th, 2009 at 2:48 am
Thanks Derek, I appreciate the code you provided to make link cloaking on my website possible and easy to do. Link cloaking is almost essential these days, and although I had purchased a link cloaker for $70 I didn’t really like how it functioned. It made link cloaking complex and time consuming.
If a customer clicks on one of my affiliate links, I want them to get to the affiliate page instantaneously, I don’t want them waiting 5 seconds for the affiliate code to anchor itself before transferring.
In my affiliate cloaking, I also didn’t want to have to use a separate file folder like go, goto, likes, recommends, etc nor did I want a separate file for each affiliate. I wanted a simple link like http://www.example.com/amazon for example, as that is such a clean, crisp, professional looking affiliate link. Most people will know that that it is an affiliate link, but it looks nice and if your providing value to the customer they won’t mind clicking on it to get to where you’re sending them.
Thanks for sharing your knowledge
Dman
January 1st, 2010 at 12:32 pm
Well this seems to be a great work, but i run an online game, with uses hundreds of different links, i wonder if there will be an easier way to cloak the url with such a high number of various links involved.
Is it a Possibility?
February 16th, 2010 at 1:25 pm
Could you please paste you htaccess file somewhere as I have the same problem as Robert here
http://www.derekbeau.com/how-to-hide-all-your-affiliate-links-with-only-one-file/#comment-12889
And really can’t find any solution. Everything works except the rewrite.