How to cloak your affiliate links

May 21st, 2013 – 40 Comments

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:

  1. 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.
  2. 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.
  3. “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:

  1. Create a folder from where you’ll serve your redirects. At Yoast we use /out/.
  2. Block the /out/ folder in your robots.txt file by adding:
    Disallow: /out/
  3. 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, 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.


40 Responses to How to cloak your affiliate links

  1. Ionut Irimia
    By Ionut Irimia on 20 June, 2013

    I’ve implented it on my website. Awesome affiliation redirect script!
    Danke Joost!

  2. Mark
    By Mark on 15 June, 2013

    This is pretty awesome. I will definitely download the files/view script and report back later.

  3. Pranjal
    By Pranjal on 13 June, 2013

    I still use Pretty link to redirect my affiliate URLs but, I think your way is far more efficient!

  4. Ruby Clarke
    By Ruby Clarke on 12 June, 2013

    Hi, das ist eine super Anleitung zum Cloaken von Affiliatelinks. Vielen Dank dafür!

  5. Joost Kaart
    By Joost Kaart on 10 June, 2013
  6. John
    By John on 9 June, 2013

    I run a small affiliate program and we made it easy for our affiliated to have “auto cloaked” affiliate links. We allow them to link to our site directly (either the home page or any other page), and we get the linking domain from the REFERRER variable. That way the links appear natural, and not as affiliate links at all, and if the affiliate so wishes they can use nofollow to keep their page rank.

  7. Keshav
    By Keshav on 8 June, 2013

    Would this not be in violation of Google Webmaster Guidelines?

  8. Nadeem
    By Nadeem on 5 June, 2013

    this concept is just great to use, redirection helps alot and its fun to do redirection.

  9. Garen Arnold
    By Garen Arnold on 5 June, 2013

    Hey Joost,
    From time to time I would also check your affiliate links to make sure no one has hacked into your website and changed your affiliate links to theirs. I have heard horror stories where people have lost commissions because people highjacked their site and changed it. Do you know of a script that will email you if any of your cloaked affiliate links have been changed? Possibly you could integrate that into your script :) Just an idea.

    Garen

  10. roshan jacob
    By roshan jacob on 31 May, 2013

    Hi,
    Very helpful or informative blog for me because I have an affiliate network.
    Thank you very much…….

  11. Jeff Guynn
    By Jeff Guynn on 26 May, 2013

    Yoast: can you recommend a script to accomplish step 3 in your redirect formula?

    • Jeff Guynn
      By Jeff Guynn on 3 June, 2013

      I’d still like a response to my post from May 26, 2013: “Yoast: can you recommend a script to accomplish step 3 in your redirect formula?” Thanks!

      • Chris
        By Chris on 6 June, 2013

        what should that script do? Think?

        • Jeff Guynn
          By Jeff Guynn on 6 June, 2013

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

          I’m referring to step 3 from above.

          • Jeff Guynn
            By Jeff Guynn on 12 June, 2013

            Oops. I guess I overlooked the link above to DL the script. My apologies. Thanks Yoast.

  12. Jacobus
    By Jacobus on 25 May, 2013

    Thanks for the great explanation Joost. As always great content. And just as a PS… since I am using your WordPress SEO for Video plugin, my traffic boomed like it never boomed before! Worth every penny!

  13. Ruthjoseph
    By Ruthjoseph on 25 May, 2013

    Hi…. Good Idea… Im going to implement this for my site… Thanks for sharing

  14. Bas van der Weerd
    By Bas van der Weerd on 24 May, 2013

    Okay,nice example but not sufficient, I’m afraid.
    These are the WordPress plugins I’m using to cloack the affiliatelinks:
    http://wordpress.org/extend/plugins/alc/
    http://wordpress.org/extend/plugins/wp-shorties/

    Also free, and they work like a charm

    • Joost de Valk
      By Joost de Valk on 24 May, 2013

      If you say “not sufficient”, I’d like an argument or I’ll call bullcrap :-)

  15. trofo
    By trofo on 24 May, 2013

    I use the Redirecter plugin to have 301 redirects. In robots.txt I use:
    Disallow: /afiliat/

    Except for the fact that the Redirecter plugin needs WordPress to be booted, do you see any othe inconvinient?

  16. Gareth
    By Gareth on 24 May, 2013

    I keep trying but it only redirects to the default (homepage) no matter which id I try, I’m only trying it with one affiliate url

  17. Steve Deane
    By Steve Deane on 24 May, 2013

    Hey Joost, Thanks for giving us that code for free. I’m just about to implement it on my site now. In fact in general thanks for all the free stuff you give the WP community. I’ve been following you for years – seriously. You are the definition of what’s good about the open source community. Keep up the good work mate!

  18. Denrrou
    By Denrrou on 23 May, 2013

    Hey, Yoast!

    Dont hate me for going a little out of topic here, but would you mind to give me a clue on how you were able to use the new genesis framework with support to html5 without breaking the style of your theme? Please? *.*

    I know i would need to rename a lot of stuff, but i thought Yoast might have a claver way to do this.

    Thanks a lot!

  19. Patrik Lommers
    By Patrik Lommers on 23 May, 2013

    Hey Joost,
    You have made a useful post for affiliate links.
    Do URL shorteners block affiliate links?
    I suggest if you are using WordPress, use Pretty Link Lite to cloak your affiliate links!

  20. Debashisa Jena
    By Debashisa Jena on 23 May, 2013

    Yeah I use a separate config file include /etc/nginx/includes/redirect.conf and then include it in sites-available

  21. Bucee
    By Bucee on 22 May, 2013

    I use two plugins for cloaking referral links: Pretty URL and Simple URL. Both of them works fine. Pretty URL offer more statistical features, but it uses more sources if we have lots cloaked links. Nowadays, I prefer Simple Urls because the redirection is faster. I always make links nofollow. I haven’t tried your mentioned technique yet.

  22. AnhDN
    By AnhDN on 22 May, 2013

    What happens if i use bit.ly, tinyurl… to cloak your affiliate links, Joost?

    • Claude "CodeAngry" Adrian
      By Claude "CodeAngry" Adrian on 22 May, 2013

      Because you want to have control over your MONEY links. Because you can’t trust third parties with your money links. Because you are not lazy regarding your money links. Because you might want to change your money links someday without editing your entire site.

      There’s more, I’ll stop here :)

  23. Andrew
    By Andrew on 22 May, 2013

    Joost, for a WordPress installation, would adding this plugin (http://www.studiopress.com/plugins/simple-urls) and then adding
    Disallow: /go/ to the robots.txt file achieve the same results as using your script?

    • Ruud Kok
      By Ruud Kok on 22 May, 2013

      @Andrew: I’ve been using the Redirection plugin (http://wordpress.org/plugins/redirection/) for this for years, which indeed achieves exactly the same, and even gives some stats on how often specific redirects were used/clicked on.

      • James Melbin
        By James Melbin on 30 May, 2013

        I use Redirection, too, and I really love it.

      • Joost de Valk
        By Joost de Valk on 22 May, 2013

        Both solutions are fine from a code perspective, in fact, I wrote about them 3 years ago, but I want my affiliate links to be as fast as humanly possible. Both of these plugins require WordPress to be booted, which, on average hosting, adds about a second or sometimes more to the redirect time, this solution is faster…

        • Will
          By Will on 18 June, 2013

          A non-plugin solution is always ideal. 98% (more like 99%) of people who write posts like this recommend software. I’m glad you didn’t go that route, and frankly, I expected you not too.

  24. Bastian Anggana
    By Bastian Anggana on 22 May, 2013

    Joost, is it necessary to add nofollow to cloaked affiliate links?

  25. Claude "CodeAngry" Adrian
    By Claude "CodeAngry" Adrian on 21 May, 2013

    Man, this is why free stuff ends up costing more time than paid alternatives. This solution is just too basic. It’s more of a Proof of Concept.

    What if they have several hundred affiliate links and those links keep piling up. Depending on the needs one can write a script that addresses a single affiliate program url structure or, if affiliate links are varied, and to many domains… there’s other tricks that can be done. Like a script that stores links to a DB and generates affiliate shortcodes.

    If I see any people interested in something more complex (in the comment here), I’ll write a script and share it. I hate giving any kind of link juice (diluted nofollow or strong dofollow) to affiliate programs. It’s like voting for your competitors and rendering yourself unnecessary in the process.

    Who needs affiliates when they got free search engine traffic… and your un-obfuscated links help them build just that.

    • Max
      By Max on 10 June, 2013

      I would definitely be interested in a script that addresses a single affiliate program url structure.

      Something that would target all links to affiliate.com/product12 and change them to affiliateplateform.com/affiliateID=2321&product=12

    • Sahil
      By Sahil on 4 June, 2013

      Great Post Joost! Masking the affiliate URLs by redirections so that people don’t find it affiliate links is definitely a good idea and plays a major role in conversions.

    • Joost de Valk
      By Joost de Valk on 21 May, 2013

      I disagree that this is too basic. If one comes here later on searching for this topic, the chance that this is enough is about 95%. The latter 5% should know better ;-)

      • Maureen O'Danu
        By Maureen O'Danu on 29 May, 2013

        Thank you, Joost. Adding the folder to the robots.txt file did the trick. The rest is done by the plugin I use, but that was not done.


Check out our must read articles about Analytics