Friday 9 December 2016

How to cloak your affiliate links


Why should I cloak my affiliate links?

If you search online for it, there are tons of reasons for people to redirect their affiliate links. The “historical” reason for cloaking affiliate links is hiding that you’re an affiliate for the search engines. Now I’m not one to think the search engines are stupid enough to be fooled by an easy script like the one I’m providing below, but there are more reasons. The funny thing is that it’s called cloaking affiliate links because of that history, but I digress. Let me give you the three reasons why I do it and why I think every affiliate should be cloaking their affiliate links:
  1. Ease of management
    You sometimes have to change your affiliate links. If you have to change them in more than one place, it’s bound to be a painful exercise, so you want to centralize management.
  2. Prevent leaking PageRank to advertisers
    While sometimes you might want to “hide” for a search engine that you’re an affiliate, for me the reasoning is actually the other way around. Affiliate links are ads, and those should be nofollowed or otherwise prevented from leaking PageRank to the advertiser. Instead of nofollowing each and every link, which is painful and you’re bound to forget at some point, why not make sure search engines can’t follow the link at all?
  3. “Clean” links
    It might be obvious when you’re linking to Amazon that the link points to Amazon, but when I link to say, Genesis, using my affiliate link, the link would point to shareasale.com, which in turn would redirect to Genesis. So the URL of the link would be ugly and wouldn’t tell you where you’re going to. Instead, my “clean”, cloaked version of that affiliate link reads:https://yoast.com/out/genesis/ That is a lot more telling about the destination isn’t it? I think it’s more user friendly.

Cloaking affiliate links, the how to

The basic process of cloaking affiliate links is simple:
  1. Create a folder from where you’ll serve your redirects, I use /out/.
  2. Block the /out/ folder in your domains robots.txt file by adding:
    Disallow: /out/
  3. Use a script in your redirect folder to redirect to your affiliate URLs.
Step 2 makes sure search engines won’t follow the redirects, but we’ll add some extra safety with our script. Step 3 could be as easy as writing each redirect in your redirect directory’s .htaccess file manually. I personally prefer something that’s a bit easier to manage, which is why I’m giving you a little redirect script I wrote below. As a bonus, this should be easier to get working on NGINX too.

Affiliate link redirect script

This script consists of three parts. The index.php file is the file that handles the actual redirects, it does a 302 redirect and sends an X-Robots-Tag header along, which makes sure the search engines that obey those headers aren’t allowed to index or follow the URL, should you forget the robots.txt exclusion.
The redirects.txt file is a comma separated file with a very simple format:
name,destination-url
For instance:
yoast,https://yoast.com
It should always have the first line:
default,http://example.com
Where you can change example.com to your domain. This will make sure that people who try to redirect to non-existing redirects get redirected to your homepage.
If you install these two files, the script will already work. With the above setup, going to /out/?id=yoast on your server would redirect you to https://yoast.com.
Now I’m a bit of a URL freak and I actually like my URLs even cleaner than that, so there’s a bit of .htaccess involved as well. It’s only 5 lines and basically makes sure that you can link to /out/yoast and that would redirect you as well. This has the added bonus of preventing people from opening your redirects.txt file, so they can’t check out all your redirects.

Neat huh? You can download the files here, or view their source here. If you run NGINX, this gist contains some sample code for how to make that work.
Let me know in the comments if and when you use it and whether you run into any issues.
Note: I’d written about this topic in the past, specifically how to do it with WordPress plugins, the Analytics bit of that post might still be very useful.


No comments:

Post a Comment