How to Properly Set Up Affiliate Links on Your WordPress Blog

When it comes to affiliate links, it’s a good idea to have them all in one logical structure so you can reuse them without looking out for them. 

This is especially important for any kind of affiliate links as each affiliate program uses a different link structure and it’s hard to remember the links. In this article I will show you how to easily set up your own link management system for WordPress.

Disclosure: Please note that some of the links in this post are affiliate links for products I use and love. This means if you click on such a link and take action (like subscribe, or make a purchase), I may receive some coffee money at no extra cost to you. This helps me creating more content free of charge to you. And, as an Amazon Associate, I earn from qualifying purchases. Thanks for your support!

I try to avoid WordPress plugins in general because they slow down the speed of my websites. These days, there are plugins for everything, even for mundane stuff you should be able to do manually.

When I was looking on the Internet for a way to manage my affiliate links, sure enough I discovered plugins for that, but I want to show you how to set this up manually.

Manual solution versus plugin

Manual management of affiliate links has one big advantage and it’s the speed. Every Plugin depends on the core code of WordPress. This means that before the plugin is able to execute itself, it needs to wait for the core to be fully loaded.

This fact adds precious seconds to the loading time of your website which has a negative effect of redirecting time as well, especially if your WordPress is running on slower hosting.

Manual management doesn’t depend on WordPress core at all, so it’s ultimately faster. On the other hand, you won’t get any statistics about the usage of your links, so if that’s what you’re after, you’re probably better off with slower solution offered by plugins.

Meet the .htaccess file

The method I’m about to show you here relies on the .htaccess feature of your web hosting provider so make sure that you can access and modify the .htaccess file.

The easiest way to check this out is to go to your File Manager and search for .htaccess file. It’s usually right inside your public\html directory within your WordPress installation. This file is hidden, as the dot at the beginning suggests, so make sure you can see hidden files.

If you can’t find this file, there are two possible reasons:

  • you just need to create it on your own and everything will work,
  • your hosting provider didn’t allow you to access this feature, in such case, ask him to make it available for you, or simply change the provider, I can assure you that Namecheap for example offers this feature automatically.

This is the link structure I chose because I think it makes sense to use natural language: todaywp.com/likes.

Some websites use /go or /out instead, but I like /likes the most. To create something similar, you need to create a new folder inside your root directory from which your website is served, typically inside the public\html directory.

I have a slightly different directory structure as I use todaywp.com as the so-called add-on domain, but that shouldn’t concern you:

Disallowing search engine crawlers

Next thing I added to the root is the robots.txt file where I wrote that the /likes/ directory shouldn’t be crawled by search engines:

Disallow: /likes/

Read this article to learn more about robots.txt file and how it works with Google Search.

Redirecting logic

Inside the /likes/ directory, I have three files, two of them courtesy of Yoast.

.htaccess file contains the rewrite rules:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>

index.php file contains the script made by Yoast:

<?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>';
}
?>

This file performs a 302 redirect. It also sends an X-Robots-Tag header to ensure that search engines will obey the noindex and no follow rules. You don’t need to understand these technical details, but they provide some extra security measure in case you forget to exclude the affiliate link in your robots.txt.

redirects.txt file contains my affiliate links:

default,https://todaywp.com
elementor, https://elementor.com/?ref=6502
elementorpro, https://elementor.com/pricing/?ref=6502
elementorhello, https://elementor.com/hello-theme/?ref=6502
grammarly, https://shareasale.com/r.cfm?b=864277&u=1342333&m=26748&urllink=&afftrack=
mailerlite, http://www.mailerlite.com/a/fu5sueh6iq
namecheap, http://shrsl.com/1s452
namecheaphosting, http://shrsl.com/1s63r
siteground, https://www.siteground.com/index.htm?afcode=f38954716e789fce916be83fd48529c8
skillshare, https://www.skillshare.com/r/user/zavreldotnet
sps-handbook, https://gum.co/sps-handbook
udemy, https://click.linksynergy.com/fs-bin/click?id=4K8EMPlpx9M&offerid=507388.81&type=3&subid=0

This is the only file you need to modify so it contains your own links and not mine 🙂

The structure is pretty simple. You should always have a default link at the top of the list to ensure that your visitors aren’t redirected to a non-existing URL.

Below this default line, you have just the short name of the product you recommend followed by comma and the appropriate affiliate link. That’s it!

Now, whenever I use the https://zavrel.net/likes/elementor link for example, I’m redirected to the appropriate affiliate link automatically, so I don’t have to remember that my referral number is 6502.

Pretty handy, right? 🙂