Improve Click-Through Rates by Hiding External Links

How to Hide External Links

Recently, I published a post about how I saved a lot of money on my smartphone bill by moving to Ting. That post used affiliate links: external links that take a user away from my site and track their origin for various reasons. I wanted a cleaner option than the URL the affiliate offered and I didn’t want to lose PageRank to the affiliates themselves, so I implemented a fairly simple redirection script on my sites.

In Yoast SEO’s post about cloaking affiliate links, Joost outlines his approach to handling external links. While he addresses monetized affiliate links specifically, I believe his approach is acceptable in other situations too. Whatever you’re thinking about hiding, this approach is solid and I’d recommend it.

However, while implementing Joost’s solution on my own site, I found myself questioning some complexities and ultimately modifying the code. I simplified his three files to just two. Here I’m sharing my version of the redirection script.

But first, the why…

Why should you hide external links?

You should totally read Yoast SEO’s post for more detail, but here are the concepts.

  1. It’s easier to manage multiple instances of an outbound link if you redirect from a common index. Think of the way CSS files can be updated once and affect the entire site. In the same way, a URL you have no control over can be updated to fix all references in one quick adjustment.
  2. For SEO purposes, it’s important to link only to quality content. If you link to an internal directory and then apply nofollow as you redirect to the external source, you maintain PageRank.
  3. A clean, readable URL is much more attractive to your visitors than what many affiliate link structures offer. You’ll probably see better click-through rates with a cleaner link.

A simpler redirect script…

You can use a plugin for pure ease of use, but not if performance matters to you. A great alternative is Yoast SEO’s script, which requires editing code whenever you want to update your list. With a few shortcuts, this is the way I’ve implemented the affiliate link redirect script on my sites.

First, decide on a directory name and create it in your site’s root. Then add this line to your root robots.txt file. You’ll need to change refer to the directory you’re using.

Disallow: /refer/

This keeps bots form crawling your redirect folder. So they simply see links pointing to an internal location and not exiting your site.

Next you’ll add these two files to the directory you created:

index.php

This is the one file to handle all redirection. Whereas Yoast SEO separates the actual list of redirect links into its own text file for ease of use, I have combined it. I figure that if you’re going to be modifying code anyway, you might as well modify the PHP file. This also potentially improves performance and reduces errors due to file permissions, because PHP doesn’t have to open the text file and read through it every time a link is clicked.

<?php
// This is your array of redirect links and shortnames or IDs
$urls = array(
	'default' => 'http://andrewmiguelez.com', // default is required in case of a bad link
	'somelink' => 'http://example.com',
	// add as many as you need...
);

$id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : '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>This redirect failed and no default was found. Please return to the previous page.</p>';
}

.htaccess

This file allows you to improve the look of your links. Rather than pointing to /refer/index.php?id=somelink we can instead point to /refer/somelink which is much nicer. This requires an Apache server, but there are alternatives for other environments.

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

That’s it. Now use it responsibly.


Any questions or problems with the code examples? Let me know in the comments below.