Recently, I entered some affiliate programs, but I didn’t cloak the links.

Yesterday, I decided to do it for some affiliate links, and in this post, I’ll show you how to do it as well.


I’ll teach you how to cloak affiliate links in WordPress, but not by using a plugin, because they negatively impact your website’s speed, and then some of you will wonder why is WordPress so slow.

According to Yoast, they can add one or two seconds to your loading time because they rely on WordPress’ code, and they need to wait for it to be fully loaded before they can execute.

So, no plugins ladies and gentlemen! We’re going to get our hands dirty in this tutorial.


Why did I finally decided to cloak some of my affiliate links

‘Cloaking’ sounds pretty bad, but it isn’t in this case.

I basically decided to cloak some of my affiliate links because some of them look scary as hell. I wanted to make them look more pretty.

Here’s an example. I’ve transformed this standard WP Rocket affiliate link from ShareASale:


Into this:

Better, right?

Now, there’s a debate whether it’s good or bad to cloak your affiliate links, but I’m not going to dig into that. There are plenty of articles on this matter on the net.

What I can tell you is that the vast majority recommends cloaking them for many reasons, one of them being the one I pointed out above.

How to cloak affiliate links in WordPress without using a plugin

Create a full website backup before doing all of this! I also recommend using a testing/staging site before implementing this on your live site!

To cloak affiliate links in WordPress, we’ll use a script.

This script is created by Yoast (thanks!), so you can rest assured that is well developed and safe for SEO.

The thing is that I didn’t really understand how to do it after reading Yoast’s post.

I had to read a couple of other tutorials to put the pieces together and figure it out.

So, I’m going to be more specific in this tutorial, so you can better understand how to do it, and to answer some of the questions that people asked.

Step 1 – Create a folder on your FTP

Access your FTP and create a folder in the root directory, which normally is /public_html/.

If you have an add-on domain, then things will normally happen in public_html/YOURDOMAIN.COM/.

This folder can be called however you wish: /out/, /go/, /refer/, and so on. I’ll use /go/ for this tutorial.

cpanel affiliate links cloaking script

Folder created in cPanel

If you’re using cPanel, just click on New Folder to create one.

Step 2 – Download and edit the affiliate links cloaking script

Go on Github and download the file on your computer.

affiliate links cloaking script github

This script is for web hosts that run on Apache, as most do (at least WordPress web hosts). If you’re using Nginx, then you should use this.

Now, unzip that archive and you’ll find 3 files:

  • An index.php file – handles the redirections;
  • A redirects.txt file – contains names/slugs and destinations;
  • A .htaccess file – this will make the links look pretty and prevent people from seeing the redirects file.

The only file you need to modify is the redirects.txt.

Initially, it will look like this:

affiliate links redirect

What’s important there is to have default, (use your own domain) at the very top!

So, remove everything and add this:

affiliate links redirect default

Change with your own domain!

If you website is on HTTP (it shouldn’t be), not HTTPS, then add http://.

Now, in that redirects file, you’ll have to add your affiliate slugs and destinations.


Let’s say you’re affiliated with a hosting company, which I’ll call “Hosting123”.

And you’ll get an affiliate link that’s ugly as sin (don’t open it; I made it up):

To cloak this affiliate link in your WordPress website and make it pretty, you’ll have to add this in your redirects.txt file, under default,


cloak affiliate links in wordpress

So, the pattern is this:


You can add whatever slugs you wish.

If you want to add hosting123-wordpress instead of hosting123, then you can do that.

Here’s how it should look when adding more slugs and destinations:

cloak affiliate links in wordpress

Do that for every affiliate link that you want to cloak in WordPress and save the file.

Step 3 – Upload the files in the folder that you created

Open your folder (/go/ in my case) and upload those 3 files that contain the affiliate cloaking scripts.

If you’re using cPanel, just open the folder and click the Upload button.

upload affiliate cloaking script in cpanel

Uploaded files via cPanel

Some of you might wonder if it’s ok to have multiple .htaccess files.

Yes, if they are in subfolders, you can. There shouldn’t be any conflicts.

Just to be extra sure, I also contacted SiteGround support (if I mentioned it, then here’s my SiteGround review :D ).

siteground ticket

Step 4 – Disallow the folder in robots.txt

We don’t want search engines to follow those redirects, so add this in your robots.txt file:

Disallow: /go/

disallow affiliate cloaking folder in robots.txt

Replace /go/ with whatever folder you have created. If you created a folder called /out/, then add /out/.

You can find your robots.txt file in the root directory – public_html.

Right-click on it, edit it by selecting Edit or Code Edit, and save it.

edit wordpress robots.txt via cpanel

Editing robots.txt via cPanel

Or, if you’re using the Yoast SEO plugin, which you should because it’s an essential WordPress plugin, then you can go to SEO -> Tools -> File Editor and edit your robots.txt file there.

yoast seo robots.txt tool

Depending on what WordPress security measures you have on your website, this feature might not be disabled.

Step 5 – Adding your cloaked affiliate links in WordPress

We’ve reached the final step.

Now, when you’ll add your affiliate links in your WordPress posts or pages, you’ll add the cloaked version, which is

If you created a folder with a different name, not /go/, then use that name in the link.

So, if we get back to the “Hosting123” example, the cloaked affiliate link would look like this:

You won’t add the ugly affiliate link anymore, you’ll add the cloaked one, which will redirect to the original affiliate link.

That’s what we did in the redirects.txt file.

Make sure you ‘nofollow’ your affiliate links as well, like this:

<a href="" target="_blank" rel="nofollow">Hosting123</a>

Your affiliates links should now be properly cloaked in WordPress. Check one out and see if it works!

How to change so many affiliate links

This process should be done from the start to avoid any hassle.

But if you’re like me and didn’t do it from the start, then you’ll have to start changing affiliate links all over the place.

Luckily, there’s an awesome WordPress plugin for that, called Better Search Replace.

This allows you to simply add an affiliate link and its replacement, and the plugin will change them!

Create a database or full website backup before doing this!

Install the plugin, then go to Tools -> Better Search Replace.

better search replace affiliate links

Step 1 – In the Search for field, add the original affiliate links that you want to replace with the cloaked, pretty ones.

Step 2 – In the Replace with field, add the new and cloaked affiliate links.

Step 3 – Most of you will have affiliate links in posts, so choose the posts table: whatevertablename_posts (this will include pages too).

Step 4 – At the bottom, if you leave Run as dry run? checked, then it will perform the search without changing anything, so you can see what it found.

I recommend using it at first, and if everything seems all right, uncheck it and perform the search and replace again.

Note that it will automatically check itself back.

Heads ups!

If you mainly work with the Text editor, as I do, but sometimes switch to the Visual editor, then some affiliate links, such as the ones from ShareASale, can suffer some modifications.

You’ll notice that they have & in them. That will change into &amp; when switched to Visual.


Will turn into this:;u=000000&amp;m=000000&amp;urllink=&amp;afftrack=

So, ‘Better Search Replace’ won’t find those because they won’t be an exact match.

Therefore, you’ll have to perform searches for both cases.

Disadvantages and alternative

While the major advantage of this method is that it’s very light and it won’t impact your WordPress website’s speed, the disadvantage is that you won’t get the features and statistics that some plugins offer.

If you really need to have certain features and statistics, then you should use an affiliate plugin.

And I have some good news!

The ThirstyAffiliates Pro plugin offers a feature which performs redirects via .htaccess (like the above method), which is a good speed improvement!

thirstyaffiliates pro plugin .htaccess redirection

Unfortunately, that option isn’t available for the free version as well, only for the Pro one.

So, if you want to use a plugin in the end, then you should at least choose ThirstyAffiliates Pro for better performance.

That’s a wrap

Hope you found this tutorial useful and comprehensive, and you’ve successfully managed to cloak affiliate links in WordPress!

Don’t forget to share the post to help out others!

You can quickly subscribe to my newsletter by using this link or the subscription form.

If you have any questions or thoughts, drop a comment or send a message via contact or Facebook page.

You can also hit the follow button on Twitter and subscribe to the YouTube channel.

If you want to start your own WordPress blog, or need a website for your business, my WordPress setup service is at your disposal!

On-going WordPress support and maintenance are on the menu as well!