How to Properly Cloak Your Affiliate Links

Published by
James Parsons
on February 24, 2016
Posted in How-to

Affiliate marketing is a great introductory way to make money from a blog, and you don’t even need a huge amount of traffic to pull it off. It’s typically higher paying and more effective than traditional advertising, and it scales well with increased traffic and a broadening niche. In fact, you can scale it up to be a million-dollar annual business at the highest levels, though that’s a matter of a decade of growth, not a year or two.

Affiliate links are tricky to use properly, however. You need to mark them clearly, both because of potential Google penalties and because of international financial laws. Thankfully, one thing you can do to help minimize their impact and maximize your profits is cloaking them.

Cloaking an affiliate link means hiding its true nature from people clicking it. Some people avoid clicking affiliate links out of spite, but will click links that don’t look like affiliates. Some forms of cloaking are black hat and can hurt your site, but if you cloak the right way, you can stay SEO friendly while still getting that bump in profits.

Reason to Cloak Affiliate Links

Cloaking is a tool, and like any tool, people use it for both good and evil. There are some legitimate reasons to cloak your links, but there are also some that just show your intent as a spam marketer.

If you’re hiding the true nature of your links because otherwise no one clicks it, you’re probably spamming and you’re not helping your case by cloaking them.

If you’re hiding the true nature of your links because users are redirected through several ad networks and offer sites before reaching their destination, that link is very clearly spam and harms the user experience. This is an awful technique, makes very little money, and you aren’t hiding anything from Google.

If you’re hiding the true nature of your links so you can send bots to one destination and human users to another, Google considers you cloaking for a malicious reason and will penalize you for doing so. This is also a bad technique, and while it’s harder to catch, it’s still obvious to some bots and will hurt you in the long run.

Pretty Affiliate URL

So what are the legitimate reasons to cloak your links through an outbound script?

  • It makes it easier to nofollow a link, by making it essentially a nofollow by default. When a link is hidden in a script like this, it won’t be parsed at all, so you don’t have to worry about sending link juice to a site you wouldn’t otherwise want to support.
  • It allows you to customize the appearance of the destination of the link. Users will hover over a link to see the URL that appears, and they will use this to inform their decision on whether or not to visit the link. Many people will avoid affiliate links or links with tracking code, so you can still catch them with outbound script links.
  • It allows you to more easily manage your links. If something changes, like a part of a URL or a tracking code, you would normally have to go through your entire site to change every instance of the link. By using an outbound script link, you only have to change that one script destination rather than every instance of the link. This also helps if an affiliate network collapses or an offer disappears; you can easily replace it with a similar offer without having to change 50 links across your site.

You might have seen me refer to this as an outbound script rather than a cloak. Cloaking the link, really, is just a secondary side effect of the method we’re putting into place. Additionally, it primarily works on sites you created on your own; you can use plugins to achieve the same effect on WordPress without the hassle, for example.

The Process

The first thing you want to do is create an outbound folder. This will be a folder in the root directory of your site, usually just called /out/ or something similar. In practice it doesn’t matter what it says, just so long as you know what it is. By root directory, I mean You can hide it deeper in sub-directories, if you really want, but that will involve modifying scripts.

Create Outbound Folder

If you want, you can create several folders, one for each major affiliate network you use. This will necessitate using several different scripts, customized for each outbound directory, but it will make your links look a little better when it’s something other than just /out/. Consider it something to assist with converting people who filter their clicks heavily. It might not be worth the time to invest in it.

The second thing is to hide that folder from scraping and from the likes of Google. A lot of your links are going to point to it, so sooner or later Google would crawl it. Given that it will be full of a bunch of redirection scripts that are pretty much solely used for cloaking, you don’t want Google looking at it. There’s nothing inherently wrong with it, it just isn’t something that needs to show up in search results.

To hide that folder, all you need to do is go to your main site robots.txt file. If you don’t have this file, it’s easy to create; just create a new .txt file in notepad. In that file, type:

User-agent: *
Disallow: /cgi-bin/
Disallow: /tmp/
Disallow: /out/

Then save the file and put it in your root directory, so that it is visible when someone types

Editing Your Robots.txt

The cgi-bin and tmp folders are typical folders full of system files that don’t need to be indexed or added to search results. Usually Google will ignore them, but not necessarily, so it’s a good idea to disallow them. You can disallow any folders of similar unnecessary content depending on your site structure. If you don’t actually have a folder, you don’t need that entry on the list; just remove the line. Obviously, if you have multiple subfolders for different links, add each of them to the list on its own line.

There’s more customization you can do with robots.txt, and you can read about it here. The main point is to never under any circumstances type “Disallow: /” and save the file. This will disallow search engine crawling on your entire site, effectively removing you from the search rankings entirely. You don’t want that to happen.

The third step is to create a PHP redirect script. I recommend using this one created by the Yoast dev Joost de Valk. It’s well-developed and well-commented if you need to modify it in any way.

Affiliate Link Script by Yoast

It is essentially three scripts, though only one of them has any serious meat to it.

  • The first file is the index.php file, and it’s the primary script. It handles your cloaking with a 302 temporary redirect status code. Don’t worry about how it works; you can parse it out if you know PHP, and if you don’t, it doesn’t really matter. Suffice to say that it makes a link to one destination redirect to another.
  • The second script is the redirects.txt file. It’s going to be the resource that the php file draws from in order to know when an incoming link should trigger the script and what link it should redirect to. The example Joost gives is just a template of what the link tag is and what URL it directs to, in the proper syntax. You can replace the keyword and the URL with whatever you want to, which will be your affiliate tag and link.

The important tags to keep in there is the default tag. Make sure that one is your site homepage. That way if someone clicks on a link with a tag that doesn’t work, they’re redirected to your homepage. Same with the sitename example. Keep those so that people can’t navigate to the redirects file itself.

Now when you want to link to an affiliate site, you will just want to make the URL a format like The IDNAME is your reference, like the default entry’s is “default.”

The process for adding a new affiliate offer, then, will look like this:

  1. Create the affiliate link with the affiliate network, so you have a typical URL. Normally this is the URL you would use for people to get your cookie.
  2. Create a reference to it in your redirects.txt file. This reference will look like “IDNAME,”.
  3. Create a new link you can use in your blog posts or landing pages. This link will look like this: Each time you want to link to this specific affiliate offer, this is the link you will use. It’s cloaked and looks like an internal link.

Now, there’s one more thing you want to do. Remember those people who look for affiliate IDs on their URLS? They’re going to see your ?id=IDNAME tag and consider that one just as bad. They don’t like anything that comes after a ? in your links. To Google, it’s also a sign of cloaking, so you want to hide that sign as well.

The way you do this is modifying your .htaccess file, or creating one if you don’t have one. This file is part of Apache web servers, so if you don’t have Apache powering your server, you will have to do this a different way. Joost recommends using this script for NGINX servers, for example.

The file will sit in your root directory as well, along with your robots.txt and redirects.txt files. Chances are it’s already there; if not, create one. Just make a txt file and change the name to .htaccess without a file extension.

All the .htaccess code does is changes the display of the URLs when you create them. It changes the ?id= into nothing, so that your .com/out/?id=IDNAME turns into .com/out/IDNAME. It looks a lot better and hides the cloak that much more.

Now, this is a limited method of cloaking your affiliate links. You can use it as much as you like, but the more you use it, the more Google will want to check it out, and the more other bots will check it out. Google isn’t dumb; they know about this method, and they will know if you’re abusing it in a black hat way. Be warned that any cloaking with abuse behind it will cause problems.

An Alternative Method

If all of that seemed too complicated, there are some other ways to cloak your links. I can’t necessarily vouch for them being any less complex or any more secure, but here they are.

  • Use a WordPress plugin to cloak links on a WordPress blog. There are a lot of different options. ThirstyAffiliates is probably the best, but you can also use Pretty Link Lite to control links in a minimalist way. Easy Affiliate Links is a little more robust, while WP Wizard Cloak attempts to automate and include some minor analytics. Links Auto Replacer is even more automation, but requires you to set rules beforehand. WooCommerce even has a plugin specifically for it as well.
  • You can use a basic URL shortener as well, though be aware that Google will follow those and see that it’s just a redirect to an affiliate site. Some people also use link unshorteners so they aren’t tricked in this method. Some shorteners also strip out anything that looks like a parameter or affiliate ID, so they won’t work.
  • You can cloak links on a WordPress blog without using a plugin as well, if you’re worried a plugin will slow down your site. This guide tells you how you can do it with just .htaccess rules.
  • You can use some simpler but less robust and less fault-controlled php code found in this guide.

Regardless of the method you choose, cloaking your affiliate links can bring a boost to your sales because of the cleanliness of your links and the lighter view Google takes on the links. They don’t like a lot of affiliate links, after all, so they’ll frown upon having to many. Cloaking them alleviates this downward pressure and helps you get that much more traffic, ranking, and profit.

Written by James Parsons

James Parsons

James is a content marketing and SEO professional who enjoys the challenge of driving sales through blogging while creating awesome and useful content.

Join the Discussion

  • dale

    When this article was written?How about fb or bing? Can i use this type of cloaker with them?