Our Top Ranked #1 Web Host for WordPress Hosting
Latest Uptime Stats (Real Records):
Server Response Speed Test Scored A+:
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.
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
To cloak affiliate links in WordPress, we’ll use a script.
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
If you have an add-on domain, then things will normally happen in
This folder can be called however you wish:
/refer/, and so on. I’ll use
/go/ for this tutorial.
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.
Now, unzip that archive and you’ll find 3 files:
index.phpfile – handles the redirections;
redirects.txtfile – contains names/slugs and destinations;
.htaccessfile – this will make the links look pretty and prevent people from seeing the redirects file.
The only file you need to modify is the
Initially, it will look like this:
What’s important there is to have
default,https://example.com (use your own domain) at the very top!
So, remove everything and add this:
https://example.com with your own domain!
If you website is on HTTP (it shouldn’t be), not HTTPS, then add
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
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:
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.
Some of you might wonder if it’s ok to have multiple
Yes, if they are in subfolders, you can. There shouldn’t be any conflicts.
Step 4 – Disallow the folder in robots.txt
We don’t want search engines to follow those redirects, so add this in your
/go/ with whatever folder you have created. If you created a folder called
/out/, then add
You can find your
robots.txt file in the root directory –
Right-click on it, edit it by selecting Edit or Code Edit, and save it.
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
Make sure you ‘nofollow’ your affiliate links as well, like this:
<a href="https://example.com/go/hosting123" 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.
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.
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
& when switched to Visual.
Will turn into this:
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!
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.
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!