How to cloak your affiliate links

We regularly review sites that monetize, in part, with affiliate links. I’ve always said and will continue to say that affiliate links should be redirected through a redirect script, but when we tried to help a client by linking to a proper explanation and example script, we couldn’t find one. So, in true Yoast style, I wrote a script and decided to write this article that outlines exactly how and why to use it.

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, 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: 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:


For instance:


It should always have the first line:


Where you can change 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

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.

Tags: runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

40 Responses

  1. Claude "CodeAngry" AdrianBy 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.

    • Joost de ValkBy 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'DanuBy 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.

    • SahilBy 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.

    • MaxBy 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 and change them to

  2. Bastian AngganaBy Bastian Anggana on 22 May, 2013

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

  3. AndrewBy Andrew on 22 May, 2013

    Joost, for a WordPress installation, would adding this plugin ( and then adding
    Disallow: /go/ to the robots.txt file achieve the same results as using your script?

    • Ruud KokBy Ruud Kok on 22 May, 2013

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

      • Joost de ValkBy 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…

        • WillBy 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.

      • James MelbinBy James Melbin on 30 May, 2013

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

  4. AnhDNBy AnhDN on 22 May, 2013

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

    • Claude "CodeAngry" AdrianBy 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 :)

  5. BuceeBy 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.

  6. Debashisa JenaBy 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

  7. Patrik LommersBy 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!

  8. DenrrouBy 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!

  9. Steve DeaneBy 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!

  10. GarethBy 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

  11. trofoBy 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?

  12. Bas van der WeerdBy 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:

    Also free, and they work like a charm

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

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

  13. RuthjosephBy Ruthjoseph on 25 May, 2013

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

  14. JacobusBy 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!

  15. Jeff GuynnBy Jeff Guynn on 26 May, 2013

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

    • Jeff GuynnBy 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!

      • ChrisBy Chris on 6 June, 2013

        what should that script do? Think?

        • Jeff GuynnBy 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 GuynnBy Jeff Guynn on 12 June, 2013

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

  16. roshan jacobBy roshan jacob on 31 May, 2013

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

  17. Garen ArnoldBy 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.


  18. NadeemBy Nadeem on 5 June, 2013

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

  19. KeshavBy Keshav on 8 June, 2013

    Would this not be in violation of Google Webmaster Guidelines?

  20. JohnBy 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.

  21. Joost KaartBy Joost Kaart on 10 June, 2013
  22. Ruby ClarkeBy Ruby Clarke on 12 June, 2013

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

  23. PranjalBy Pranjal on 13 June, 2013

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

  24. MarkBy Mark on 15 June, 2013

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

  25. Ionut IrimiaBy Ionut Irimia on 20 June, 2013

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