How to cloak your affiliate links
We used to consult for sites that monetize, in part, with affiliate links. We normally advised people to redirect affiliate links. In the past, we noticed that there wasn’t a proper script available online that could handle this for us, so we created one to tackle this problem. In this post, I explain how you can get your hands on it and how you can get it running on your website.
Why should I cloak my affiliate links?
A quick online search will result in tons of reasons as to why you should redirect your affiliate links. The “historical” reason for this is hiding from search engines that you’re an affiliate. It would be naive to think that search engines don’t understand what’s happening, but nevertheless, this seems like a valid reason.
There are also a few more advantages to cloaking your affiliate links, such as:
- Ease of management
Sometimes you might need to change your affiliate links. If said links are spread out across your blog, this could become a quite time-intensive task. By centralizing the affiliate links, you have one location to manage all of them. - Prevents leaking PageRank to advertisers
Affiliate links are ads and should be nofollowed or otherwise altered to prevent leaking PageRank to the advertiser. Instead of having to do this manually for every individual affiliate link, you can do this is a single location without much hassle. This also prevents the possibility of forgetting to add nofollow to one of the links. - “Clean” links
Different affiliate programs tend to use different permalink structures. Some might have relatively ‘clean’ links, whereas others tend to add a lot of gibberish. Using the redirect script can help you deal with this issue because the cloaked URL will always follow the same structure. This makes it a lot clearer for the user where the link is taking them to!
Cloaking affiliate links, the how-to
The basic process of cloaking affiliate links is simple:
- Create a folder from where you’ll serve your redirects. At Yoast, we use /out/.
- Block the /out/ folder in your robots.txt file by adding:
Disallow: /out/
- Use a script in your redirect folder to redirect to your affiliate URLs.
Step 2 ensures search engines won’t follow the redirects, but we’ll add some extra security measures in our script to prevent accidental indexing of our affiliate links. Step 3 is as easy as manually adding each redirect to your redirect directory’s .htaccess file, assuming you’re running your website on an Apache-based server. Alternatively, you can use the script we produced to make it easier on yourself. The added bonus of this script is that it also works for servers running Nginx!
Affiliate link redirect script
The script we created consists of three files, one of which is optional: an index.php file, a redirects.txt file and, to finish it all off, a .htaccess file to prettify your URLs.
Index.php
This file contains the logic that handles the actual redirection by performing a 302 redirect. Additionally, it sends a X-Robots-Tag header along to ensure search engines that can detect this header, obey the noindex, nofollow rules we pass along in it. We do this as an extra security measure in case you might forget to exclude the affiliate link in your robots.txt.
Redirects.txt
The redirects.txt file is a comma-separated file that contains a list of names and destination URLs like so:
yoast,https://yoast.com
Note that the file should always contain the following line at the very top to ensure people don’t attempt to redirect themselves to a non-existing URL:
default,http://example.com
Just change example.com to your own domain and you’re ready to go!
.htaccess
If you only install the above two files, you’ll already have enough in place to get things running. However, we advise you prettify the URLs because this dramatically increases the readability. Without prettifying your URLs, you’ll end up with something like /out/?id=yoast instead of /out/yoast.
Prettifying can be achieved by adding a .htaccess file to the mix. This small file also helps ensure people can’t access your redirects.txt file to take a peek and see what affiliate links are available.
What about plugins?
In the past, we’ve received questions about using WordPress plugins to tackle this cloaking issue. Despite there being a lot of valid options, they have one small caveat: speed. Because these plugins depend on WordPress’ core code, they need to wait for it to be fully booted before being able to execute themselves. This can easily add a second or two to the total loading and redirecting time if you’re on a slow server.
Our non-plugin solution is faster because it doesn’t depend on WordPress to run.
Ultimately, the best option depends on your needs. If you want to collect statistics on your affiliate links, you might be better off with a plugin. Otherwise, just use our script to keep things fast.
The files
If you’re interested in running this nifty script on your own website, you can download the files. Feeling adventurous? You can find the source code on GitHub. People running Nginx can find sample code in this gist to see how to make it work for them.
Read more: Playing with the X-Robots-Tag HTTP header »
Coming up next!
-
Event
Wordcamp Asia 2025
February 20 - 22, 2025 Team Yoast is at Attending, Sponsoring, Yoast Booth Wordcamp Asia 2025! Click through to see who will be there, what we will do, and more! See where you can find us next » -
SEO webinar
Webinar: How to start with SEO (January 16, 2025)
16 January 2025 Learn how to start your SEO journey the right way with our free webinar. Get practical tips and answers to all your questions in the live Q&A! All Yoast SEO webinars »
Interesting.. Are there any potential downsides to this?
Is this something that in the future Google might decide that they don’t like?
This is one of the best explanation for cloaking affiliate links. I have been using plugins to do redirects but from now on I will use a custom script like mentioned here.
Cool article, but it’s way easier to use ‘pretty links’ plugin, surely?
Very valuable to know. Instagram wouldn’t let me have affiliate link until I used this technique. Keep up the good work!
Wouldn’t this create a false bounce rate? As all clickthrougs on the first pageview would make it a bounce?
I have used a slightly different method for several years that works similarly. What I haven’t done is blocked the folder in my robots.txt file. (I’ve been doing it manually, but blocking at the source is more elegant and foolproof).
I notice the /out/ folder is not blocked in your robot.text file. Is this from a recent change, or no longer needed?
Hi,
this is a clever way! In the past I even noticed some of my cloaked links appearing in the search engines and stats, did not quite block something properly :-) But, there is a caveat with Amazon, as they are not a big fan of cloaked links.
They do allow something like EasyAzonPro to operate, but that plugin does show you are being taken to Amazon (when hovering). With your method I am not sure Amazon would approve, unless a site owner makes it really clear the link is going to Amazon (which is their main concern).
Yep, that is correct. I spent a few hours reading all their Associates agreements and they say it needs to be clear that you are linking to amazon. Else you break their Tos.
Surprisingly though: If you use amazons own site stripe quick link method you see something like this while hovering – http://amzn.to/2jdsKaw. Weird heh?
You failed to mention pixel tracking which I find doesn’t work with a redirect script.
Hi Yoast,
Nice and handy little script.
I got it running on my site now but can’t seem to make the links prettier via the .htaccess code. When I implement that I receive a 404 error. So using https://www.lenzencenter.nl/out/?id=lenson works, but I can’t get the link https://www.lenzencenter.nl/out/lenson pointing to the Lenson site.
I wonder what can be the problem. Amongst others I have WP Rocket running. Could this be causing my problem? How can I best test to get the pretty links working?
Cheers,
Marc