How to Cloak Affiliate Links in WordPress (with and without Plugins)
If you are an affiliate marketer, you probably know the importance of masking your affiliate links. However, not everyone knows how to do it correctly. In this guide, we will show you different methods to cloak affiliate links in WordPress with and without plugins.
Before jumping into the different ways to do it, let’s have a look at the benefits of cloaking affiliate links.
Why Cloak WordPress Affiliate Links?
Cloaking affiliate links is a popular method used by most marketers and bloggers. It helps them hide their unique affiliate ID and keep the affiliate link short, making the link more secure and user-friendly. On top of that, affiliate link cloaking can also help you with link management. If you have an established site and are working with several affiliate partners, managing all the affiliate links manually and inserting them into your articles can be tedious and time-consuming.
However, you can use dedicated link cloaking plugins to help you manage all the links. These tools allow you to change the URLs of your affiliate links from a single place as well as track the clicks they receive.
All in all, the main advantages we mentioned above are:
- Protect affiliate account
- Shorten affiliate links
- Better link management
Let’s have a closer look at each benefit.
Hide Affiliate ID
If you use your affiliate link on your site, you will expose your unique affiliate ID. This isn’t recommended because someone can violate the terms and conditions of the affiliate company using your affiliate ID and get your account banned.
For example, one of your competitors may publish your affiliate link on forums, which may violate the terms of the affiliate company. Not only you may get your affiliate account suspended this way, but also they may freeze the money you have in your affiliate account.
To prevent it, it’s a good idea to mask your unique affiliate ID. The best way to do that is to use dedicated affiliate plugins.
Shorten Affiliate Links
Most affiliate links look like this: www.domain.com/product-name/ref=1234?utm_medium=affiliate
As you can see, it’s quite long and ugly and not the best-looking link to share on social media, blog posts, or emails. Instead of using services like bit.ly, to make it shorter and more user-friendly, you can simply cloak your links as we’ll see in the next section.
Link Management
If you work with several affiliates, inserting and managing the links can be quite tricky. However, that’s much easier with a link cloaking plugin that allows you to easily change the URL of the links and add them to your articles. Additionally, you can categorize your affiliate links for better organization and easier management.
Now you know its importance, let’s see how to properly cloak affiliate links.
How to Cloak Affiliate Links in WordPress
There are two main ways to cloak affiliate links in WordPress:
- Using a dedicated plugin
- Manually
- By editing the .htaccess file
- Editing the index.php file
Let’s have a look at all these methods, so you can pick the most appropriate one for you.
1) Cloak affiliate links using plugins
The easiest way to cloak affiliate links is to use a dedicated plugin. There are several cloaking plugins out there, but we recommend you use some of the ones we mentioned on this list. For this tutorial, we are going to use ThirstyAffiliates.
1.1) Installation and Activation
The first thing you need to do is install and activate ThirstyAffiliates on your site. The plugin has both free and premium versions and for this demo, we will use the free version.
On your WordPress dashboard, go to Plugins > Add New and search for the plugin. Then install it and activate it.
After activation, you will see the plugin’s settings on the left-hand side from where you can start setting it up.
1.2) Plugin Configuration
Before starting to cloak links, you have to modify some of the settings. In this section, we will show you some of the options you need to check before importing your affiliate links.
First, go to ThirstyAffiliates > Settings. Let’s have a quick look at each tab.
1.2.1) General settings
The General tab contains some of the basic configuration options such as:
- Link insertion type
- ThirstyAffiliates button configuration
- Stats clearing
- Disable IP address collection
- Bot blocking
- Bot crawl blocker script enabling
Simply select the options you prefer. In most cases, the default is ok. Make sure you enable the Bot Crawl Blocker Script to prevent search engines from indexing your affiliate links.
Once you are done with the general settings, save the changes and move to the next tab.
1.2.2) Link appearance settings
The Link Appearance settings are the core configuration of ThirstyAffiliates. Before starting affiliate link cloaking, you must tweak these options.
First, configure the link prefix as it is an important part of your affiliate links. Simply select your preferred option from the dropdown.
You can also create a custom link prefix from there.
After that, choose the redirection type. 302 is a good option for temporary redirections. If you aren’t familiar with the different types of redirection, have a look at this post.
Finally, you can style your affiliate links by adding custom CSS classes and some CSS code.
Once you’re happy with your configuration, move to the next tab.
1.2.3) Modules settings
The Modules tab allows you to enable/disable specific features such as statistics, link fixer to make sure your links work, and uncloak links to uncloak specific links.
Simply select the options you want and save the changes.
1.2.4) Import/Export
The Import/Export features are useful to move the plugin’s settings or import affiliate links from another website.
ThirstyAffiliates generates unique codes for each WordPress installation. When you have to move the same settings to another site, you can simply use this code. All you need to do is copy the code, go to the plugin’s settings on the second website, paste the code and import the settings. As simple as that. This way, you can easily transfer settings between sites.
Now that you have completed the basic configuration, let’s see how to cloak links and add them to posts.
1.3) Link Cloaking
Once you’re happy with the plugin’s settings, you can start cloaking the affiliate links. In this section, we will show you how to properly cloak links in WordPress.
First, go to ThirstyAffiliate > Affiliate Links and press the New Affiliate Link button to add an affiliate link.
The plugin will redirect you to the affiliate link cloaking wizard.
First, name your affiliate link. This would come right after the prefix. For example, if you want your affiliate link to be https://www.example.com/go/quadlayers, you have to write QuadLayers in the title.
Then, enter the Destination URL that will be the direct affiliate link.
To keep your links organized, you can create link categories and add the link to them. This can be a helpful feature if you have many affiliate links.
On top of that, you can add a banner image of the affiliate product using the Upload/Insert or Add external image options.
You can also use the link options section on the right-hand side to decide whether you want the link to be follow or nofollow, open it in a new window, and more.
Once you are happy with the changes, save the link. The plugin will give you the final cloaked URL that you can edit.
That’s it! That’s how you can easily cloak affiliate links in WordPress with a plugin. Now it’s time to insert it into your content.
1.4) Inserting Cloaked Links
Once you have cloaked affiliate links, you can start adding them to your posts and pages. The good news is that you don’t need to copy and paste them manually. ThirstyAffiliates comes with in-built options to easily add links.
Let’s see how to insert the links using both the Classic and Gutenberg Editor.
1.4.1) Classic Editor
First, open the post you want to add the link to and you will see two new options as shown below.
The first option lets you insert an affiliate link into the article while with the second button, you can create new affiliate links through the editor. For now, let’s click on the first button.
Select any part of your post and press the first TA button. It will display a search box where you have to search and select the affiliate link you want to include.
After adding the link, publish (or update) the post.
1.4.2) Gutenberg Editor
If you are using the brand new Gutenberg editor, follow these steps.
Open the post you want to add your affiliate link to, select the keyword you want to link, and choose the Affiliate Link option from the dropdown.
Then, search for your affiliate link and add it to the post.
Finally, post or update the article and that’s it.
As you can see, cloaking and adding affiliate links using the ThirstyAffiliates plugin is quite easy. However, if you don’t want to use third-party tools, you can also cloak affiliate links manually.
2) Cloak WordPress Affiliate Links Manually
You can also cloak affiliate links manually editing some core files. In this section, we will show 2 different ways to do it:
- Edit the .htaccess file
- Edit the index.php file
NOTE: As we’ll modify core files, we recommend you do a complete backup of your site. Additionally, make sure you use a child plugin. You can either create one or use any of these dedicated child theme plugins.
2.1) Edit the .htaccess file
Editing the .htaccess is straightforward. If you’re using an SEO plugin such as Yoast, Rank Math, or All in One SEO, you can do it from the WordPress dashboard without using any FTP client. For this tutorial, we will use Yoast SEO.
In your admin dashboard, head to SEO > Tools and select the File Editor option.
There, you will see your website’s essential files such as robots.txt and .htaccess. Let’s see how to modify the .htaccess file through the settings.
Before changing the file, you need to prepare your affiliate link list using the following format:
## Link Cloaking ##
Redirect 302 /linkout/affiliate1 https://www.example1.com/ref=quadlayers
Redirect 302 /linkout/affiliate2 https://www.example2.com/ref=quadlayers
Redirect 302 /linkout/affiliate3 https://www.example2.com/ref=quadlayers
As you can see, we are using the 302 redirection type with the affiliate link prefix /linkout/. After /linkout/, you need to enter the affiliate name and finally add the destination URL.
For example, here’s a sample code that will redirect visitors to our website:
Redirect 302 /linkout/quadlayers https://www.quadlayers.com/ref=affiliateID
When you paste this code in the .htaccess file, you will be able to access quadlayers.com by typing https://www.domain.com/linkout/quadlayers.
Use the above code and customize it with your affiliate links. After that, paste the links right above the line that says #END WordPress and save the changes.
That’s it! From now on, you can use the cloaked URLs in your blog posts and pages. When a user clicks on them, the .htaccess file will manage the redirection and send the user to the destination URL you’ve specified. You can test the code by entering the cloaked URL into your browser and checking where it takes you.
2.2) Edit the Index.php file
If you don’t feel comfortable editing the .htaccess file, you can create a dedicated folder for redirecting. In this section, we will show you how to cloak your affiliate links manually in WordPress using a more advanced method. For this, you need to use a reliable tool such as FileZilla or File Manager. For this tutorial, we will use the File Manager plugin because it’s easy to use and needs minimal configuration.
First, head to Plugins > Add New to install and activate the File Manager plugin on your site.
On the left-hand side, you will see the plugin’s settings under WP File Manager.
Once you’re there, you need to create a folder inside the public_html folder. You should name the folder with the prefix you will use in the URL. For example, if you want to use /go/ as the affiliate link prefix, the name of the folder should be “go”.
Right-click anywhere in the public_html folder and select New folder.
Add a name to the folder using the affiliate link prefix as explained above. In this case, we’ll use go.
Now open the folder. First, you need to create a PHP and a txt file in it. Then, you will need to create a file called index.php using any note editor and paste the following snippet inside the new index.php file.
<?php $id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default'; $f = fopen( 'redirects.txt', 'r' ); $urls = array(); // The file didn't open correctly. if ( !$f ) { echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.'; die; } // Read the input file and parse it into an array while( $data = fgetcsv( $f ) ) { if ( !isset( $data[0] ) || !isset( $data[1] ) ) continue; $key = trim( $data[0] ); $val = trim( $data[1] ); $urls[ $key ] = $val; } // Check if the given ID is set, if it is, set the URL to that, if not, default $url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false ); if ( $url ) { header( "X-Robots-Tag: noindex, nofollow", true ); header( "Location: " . $url, 302 ); die; } else { echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p> <pre>default,http://example.com</pre> <p>Where you should replace example.com with your domain.</p>'; }
After that, upload this file to the go folder.
Now it’s time to add the affiliate links to a txt file. From the same wizard, create a new txt file and call it redirects.
Open it using the Code Editor.
Use this format to add affiliate links:
default,https://www.yourdomain.com
affiliate1,http://affiliate1.com/ref=quadlayers
affiliate2,http://affiliate2.com/ref=quadlayers
affiliate3,http://affiliate3.com/ref=quadlayers
The first part of the code represents the cloaked slug while the second part describes the destination URL.
Make sure you customize the first line and replace yourdomain.com with your domain as well as the destination URL.
As you can see, we use commas (,) to separate these sections. This way, you can modify the redirects.txt file and add/remove affiliate links. Once you are done editing the file, save it.
From now on, you can use the slugs mentioned in the txt file to automatically redirect users to the destination URLs.
The next step is to stop search engines from accessing the newly created folder and files. To prevent this, you have to modify the robots.txt file by adding this code.
Disallow: /folder-name/
Simply replace /folder-name/ with your folder name and then use an SEO plugin to update the robots.txt file.
In our case, the code will be:
Disallow: /go/
Remember to save the file after modifying it.
Finally, to embed affiliate links you have to use the following format:
https://www.yourdomain.com/folder-name/slug
In our case, the cloaked URL will be:
http://jane.local/go/affiliate1
That’s it! That’s how you can cloak affiliate links in WordPress using the index.php file.
Conclusion
In summary, cloaking affiliate links helps you protect your affiliate account, shorten affiliate links to make them more user-friendly, and better organize and manage the links.
In this guide, we’ve seen two methods to cloak affiliate links in WordPress:
- With a plugin
- Manually
- .htaccess file
- .index.php file
You may be wondering “which method should I use?” It depends on your skills and needs. The plugin method is perfect for beginners or users who don’t want to edit core files. With a dedicated tool like ThirstyAffiliates, the process is super simple and the setup process is quite easy. Additionally, it’s a great tool to track link clicks.
On the other hand, if you’re a more advanced user, you can cloak affiliate links manually. Apart from editing core files, this method makes managing the links more complicated compared to the plugin because you need to enter the links manually to your posts and pages every time. If you want to add many links, it can be time-consuming.
Considering all this, we recommend using a dedicated plugin. Cloaking new links, updating existing ones, and categorizing them is much easier using a plugin. On top of that, they also come with reports and other useful features. If you’re looking for the best affiliate link cloaking plugins, have a look at this post.
Which method are you going to use to cloak links on your site? Do you know of any other that we should include? Let us know in the comments below.
2 Comments
Add comment Cancel reply
You must be logged in to post a comment.
I used to do this by editing .htaccess file and I didn’t know about the index.php file method. Thanks for let me informed about this.
You’re welcome Mira