One of the easiest and most lucrative ways to monetize a blog is through affiliate links and affiliate networks; the modern iteration of lining your site with advertisements. There are a number of very good reasons to clearly mark your affiliate links, not the least of which because it’s a quick way to earn a Google penalty and a loss of trust. Affiliate links were once the domain of black hats almost entirely, but they are quite legitimate when used properly. In fact, you can even cloak them in a legitimate way, a way that won’t kill your SEO.
Why Cloak Affiliate Links?
There are both good and bad reasons to want to . First, the bad; if you’re cloaking for any of these reasons, you’re risking a serious penalty.
• Hiding the URL because users would otherwise never click it.
• Hiding the URL because when a user clicks it, they are redirected through multiple ads before reaching the destination.
• Splitting web traffic between bots and humans to redirect link juice.
So why might you want to cloak your URLs legitimately?
• For ease of management. Cloaking URLs through a script lets you assign a single instance of a link instead of requiring you to go through every instance on your site when you need to change the destination.
• Nofollowing advertiser links. Naturally, you don’t want to send your hard-earned PageRank juice to an advertiser’s side; you just want to direct clicks through. Instead of manually nofollowing every link, run them through a cloaking script so they aren’t parsed at all.
• Usability with link destinations. When you hover over a link in your browser, it shows the destination URL. Many users read this destination to determine where they’re going and if they want to go there. With an affiliate link, you’re often passed through a gateway page before reaching the actual content; allows you to show what the actual content at the destination rather than the intermediary ad.
So, how do you set up an SEO-friendly redirect?
Step 1: Create an Out Folder
This step is simple. In your site root directory, create an outbound folder, such as /out/. This folder will hold your scripts and outbound links. This is necessary because of how you’re going to secure your redirect cloaking against search engines.
Step 2: Hide the Folder
Specifically, you’re going to deny access to this folder. In your robots.txt file, disallow the entire /out/ folder. Make sure you’re not accidentally adding Disallow: / to your list, which will block your entire site and ruin your SEO.
The purpose of this step is to keep Google and the other search engines from pulling and indexing your redirects and scripts. If Google finds them and doesn’t properly understand their purpose – and your legitimate intentions for cloaking – you can find yourself penalized for using them.
Step 3: Create a PHP Redirect File
The files used in this example were created by Yoast, of the WordPress SEO Plugin fame. You can find them here: https://gist.github.com/jdevalk/5622742. Here’s what each of them do.
The index.php file is the meat and bones of your . It performs a 302 redirect, which is a temporary move status code according to HTTP status codes. This is opposed to a 301, which is a permanent redirect, which you don’t want to use for URL cloaking. 302s are often used to tell Google to prefer shorter URLs rather than longer URLs with session IDs and other parameters. It’s sort of like an older version of canonicalization.
The PHP file has an additional layer of security in a robots header, which provides a secondary bit of protection against indexing, just in case your robots.txt is malformed or ignored. It’s essentially the same as appending noindex, nofollow to every redirect link.
Third, and most importantly, the PHP file reads the next file you’ll set up to find the URL you want to redirect to.
Step 4: Set Up Redirects.txt
The Redirects.txt file is your resource for the PHP script. Essentially, it acts as a database for your redirects. You put in a name and a destination, comma-separated, for each site redirect you want to use. You will need two default entries:
These are even more additional protection against unwanted redirects. If a user clicks a redirected link to a source that’s not specified, they are directed back to your homepage. Likewise, if they try to access your redirects file, they will be redirected to your homepage.
Now, each time you set up an affiliate link, rather than post the link as a normal link, you link to a special URL in this format:
The URLID in this case would be default, yoursitename, or whatever other cloaked URL you want the content to show up as. The ID has to reference an ID in your , otherwise it will return to default and direct to your homepage.
Step 5: Modify .htaccess
The script works with just those two files, but the result is a little messy. Having an ?id=variable at the end of your URLs looks messy and is a flag that a script is in use. To avoid this mess, you can create and edit a .htaccess file in the /out/ folder. Again, a sample version of the file – all five lines of it – is posted with the other two files on the Yoast example.
What this file does is converts any URL with the ?id=variable to a more user-readable benign url. For example:
• becomes http://example.com/out/redirect1
This makes your URLs a lot cleaner, without compromising your SEO integrity.
Obviously, be sure to limit your use of this script to legitimate purposes. It’s easy to cloak URLs you shouldn’t, and you for them. Intention is everything; use them for a beneficial user experience, while marking your affiliate links in other ways, and you should suffer no ill effects.