What’s covered in this post
In this post I cover how to set up 301 redirects for affiliate links quickly and efficiently without the use of any plugin or redirect script using only the built in functionality of Nginx.
Why you should cloak your affiliate links
Analytics
I love numbers and the most accurate way to track if a link is clicked is by logging each click. If you’re serous about making money off of your affiliate links you’re going to want to know how much each click is worth for each program.
If you are relying on something like Google analytics, which is dependent on JavaScript being run on your web site, you could be missing a significant amount of data. You will not be seeing links that were shared on twitter, via email or for users that have disabled JavaScript. The method I’m going to outline will log every click to an affiliate link via your servers access logs.
Search Engine Optimization (SEO)
Google and other search engines will penalizes sites for using affiliate links that are not marked as “nofollow” links. This solution will use a robots.txt file to disallow any following.
Easy to remember
A short link like “https://r.odinsql.com/digitalocean” is easier to remember than “https://www.digitalocean.com/?refcode=f1ad4e30b2a2”
Easier to update
Lets say that one of the affiliate programs that you’re taking part of has a special offer at a different URL that will result in a larger number of conversions that you’re usually getting. It’s a whole lot easier to update the link in a centralized location than to have to go through every blog post and update each link individually.
Why I use Nginx for affiliate link redirection
Performance
Nginx is incredibly fast at serving redirects and logging then. In the world on affiliate marketing even a few milliseconds of extra processing time can end up costing you money. You won’t find a script or plugin that will handle redirects as quickly as Nginx natively can.
Log analysis
Each click will be logged and can later be analyzed using a log analysis tool such as AWStats.
It’s free
If you already have an Nginx web server there’s no need to pay for a plugin or or script.
How to use Nginx for affiliate link cloaking
Create a sub-domain
First we’ll need to create a sub-domain used exclusively for redirection. In my case I’ve created the sub domain r.odinsql.com
This needs to be done with whom ever is hosting your DNS records. I use CloudFlare to host my DNS records. If you’re not already using a free CloudFlare for content distribution I highly recommend them.
Create a website for the new sub-domain
If you need instructions on how to configure and setup a website using Nginx take a look at my tutorial on how to do it with EasyEngine. EasyEngine is literally the easiest and most fool proof way of setting up a new website on a VPS.
Create a robots.txt file
This new website for your sub-domain is going to have a single file “robots.txt”
This is what your robots.txt should look like.
User-agent: * Disallow: /
This tells the search engines that you don’t want them accessing anything on this site.
Put your redirect links in the new sites Nginx config file
This is what the actual config file for r.odinsql.com looks like
# HTML NGINX CONFIGURATION server { listen 80; listen [::]:80; server_name r.odinsql.com; access_log /var/log/nginx/r.odinsql.com.access.log; error_log /var/log/nginx/r.odinsql.com.error.log; root /var/www/r.odinsql.com/htdocs; index index.html index.htm; error_page 404 = @foobar; error_page 403 = @foobar; location / { try_files $uri $uri/ /index.html; } location @foobar { return 301 https://odinsql.com; } location /hireme { return 301 https://www.elance.com/s/benjaminfredrick/; } #cloudflare location /cloudflare { return 301 https://cloudflare.com; } #hosting sites #linode location /linode { return 301 https://www.linode.com/?r=dc324ab41e66f3facfcc6eff74acfe74a414e739; } location /linode-n { return 301 https://www.linode.com/; } #digitalocean location /digitalocean { return 301 https://www.digitalocean.com/?refcode=f1ad4e30b2a2; } location /digitalocean-n { return 301 https://www.digitalocean.com; } #vultr location /vultr { return 301 http://www.vultr.com/?ref=6817742; } location /vultr-n { return 301 http://www.vultr.com; } location /cloudways { return 301 http://www.cloudways.com/en/?id=18942; } location /bluehost { return 301 http://www.bluehost.com/track/odinsql/; } location /amazon { return 301 http://www.amazon.com; } #ramnode location /ramnode { return 301 http://www.ramnode.com; } #sendgrid location /sendgrid { return 301 http://mbsy.co/sendgrid/17518355; } #loaderio location /loaderio { return 301 https://loader.io/s/xewK7; } location /loaderio-n { return 301 https://loader.io; } #amazon location /amazon-ec2 { return 301 http://aws.amazon.com/ec2/?_encoding=UTF8&camp=1789&creative=9325&linkCode=ur2&tag=o0c22-20&linkId=744HAUET442HAPV4; } }
I’ve created a location block for each affiliate link. The location is just a short name that I use for the affiliate link followed by a 301 redirect to the actual affiliate URL. Any 404 or 403 errors result in a redirect back to my blog odinsql.com. If I need to add or edit a link I open the config edit it and then run
sudo service nginx reload
This reloads any updates that I’ve made to the config into Nginx.
For now I don’t have that many affiliate links. As I continue to blog and add more affiliate links I may decide to break this config file into separate smaller includes. For now it’s a quick and easy solution that’s fast and free.
If you’re in need of a quality VPS host to test this out on I would recommend Vultr. TVultr offers low cost high performance VPS with SSD whit hourly and monthly billing options available.
That’s it for this post it might be a bit too technical of the average affiliate marketer but it’s how I cloak affiliate links. If you have a question about this please leave me a comment. If you would like to stay updated regarding this blog join my mailing list by filling out the form on the right hand navigation.