How to cloak your affiliate links

Joost de Valk is the founder and CEO of Yoast. He's a WordPress / Web developer, SEO and an Open Source fanatic.

We regularly consult for sites that monetize, in part, with affiliate links. We usually advise 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 indexation 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.

11 Responses to How to cloak your affiliate links

Is this something that in the future Google might decide that they don’t like?

Rahul Ghosh • 2 years ago

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.

Arnold Knight • 2 years ago

You’ve given us a chance to get help whenever we need it, you Friends at http://www.yoast.com Most people don’t use www. anymore in front of a website address, but I still do (but not always).. Here’s my website address http://www.youarnoldemanuelknight.com
You may suggest what it is that I should do next with it. Thank you very much for the good things that you do for all of us.

Mike • 2 years ago

Cool article, but it’s way easier to use ‘pretty links’ plugin, surely?

LED Razsvetljava • 2 years ago

Very valuable to know. Instagram wouldn’t let me have affiliate link until I used this technique. Keep up the good work!

Rob • 2 years ago

Wouldn’t this create a false bounce rate? As all clickthrougs on the first pageview would make it a bounce?

Ryan Guina • 2 years ago

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?

Jacco • 2 years ago

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).

Femke • 2 years ago

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?

Brandon • 2 years ago

You failed to mention pixel tracking which I find doesn’t work with a redirect script.