How to cloak your affiliate links
We regularly review 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:
- 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.
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.
The redirects.txt file is a comma-separated file that contains a list of names and destination URLs like so:
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:
Just change example.com to your own domain and you’re ready to go!
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.
If you’re interested in running this nifty script on your own website, head on over to GitHub. Feeling adventurous? You can find the source code here. People running Nginx can find sample code in this gist to see how to make it work for them.