DIY Link Cloaking, With Pretty URLs

There are plenty of ways to spend money to do link cloaking, and there are tutorials that talk about ways to do it yourself. But I haven’t seen a good tutorial that actually takes you through, step by step, how to do link cloaking yourself if you aren’t already a programmer.

So I decided to write one.

The link cloaking described in this post will be of the “pretty” form, e.g. If you’re writing it yourself, there’s no sense having an ugly URL when you can just as easily have a nice one.

Dot htaccess

The first step is to edit the .htaccess file on your web server.

This is a moderately dangerous thing to do. Screwing up your .htaccess file can take down your entire web site. Luckily, you have a guide who has done just that in the past, and will do his best to avoid having you do the same.

Nevertheless, step 1 is to download a copy of your .htaccess file to your computer now! That way you have an original copy of it to upload to your server if any of the changes we’ll make screws up your site.

The changes you make will depend on what your .htaccess file looks like now. If you have WordPress on the site, it probably looks like this:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Everything between the comments (the lines that start with #) are automatically generated by WordPress. Don’t touch those bits or remove the comments!

To add DIY link cloaking to a WordPress powered site, add the following line of code after the “# END WordPress” comment.

RewriteRule ^recommends/([^\./]+)$ cloak.php?req=$1 [L]

The word “recommends” can be changed to whatever word you want to use. If you want to use a different word depending on what product you’re linking to, copy the RewriteRule line multiple times. For example, these lines allow me to use both “recommends” and “endorses”.

RewriteRule ^recommends/([^\./]+)$ cloak.php?req=$1 [L]
RewriteRule ^endorses/([^\./]+)$ cloak.php?req=$1 [L]

If this does not work, if you get a File Not Found page showing up in your blog, try removing the ^ from just in front of the word, so you would have:

RewriteRule recommends/([^\./]+)$ cloak.php?req=$1 [L]
RewriteRule endorses/([^\./]+)$ cloak.php?req=$1 [L]

If you don’t have WordPress, you may also have to add these lines just before the RewriteRules:

RewriteEngine On
RewriteBase /

Don’t add those if they already exist in your .htaccess, but do add them if they’re missing.

So a non-Wordpress .htaccess file might look like this:

RewriteEngine On
RewriteBase /

RewriteRule ^recommends/([^\./]+)$ cloak.php?req=$1 [L]
RewriteRule ^endorses/([^\./]+)$ cloak.php?req=$1 [L]

Basically, these rules tell the server that anytime a web surfer tries to go to /recommends/blah on your site, to instead call the cloak.php file and pass in an argument named req that has the value “blah”.

Writing cloak.php

The next step is to write the cloak.php program. Luckily, I’ve already done this for you, so you can copy and paste the following into a file named cloak.php and upload it to the main directory of your website (the same directory where your .htaccess file was).

This has been kept deliberately simple so non-programmers can work with it. Where the file has “blah” or “GreatNewOpp” put whatever you want at the end of your cloaked URL (generally the name of whatever program you’re promoting). Also, on the header line change the URL to the URL of where they should really go (generally an affiliate link of some sort).

If you want more than two cloaked links, copy and paste one of the if/header pairs and modify the text and the URL for the new cloaked link. Upload the new file to your server and you’re ready to go.

What This Doesn’t Do

This does nothing for ad tracking, which requires more complex code. You can slot in ad tracking easily enough, though, by forwarding the cloaked link to an ad tracking link, and then the ad tracking link forwards to your final affiliate URL.

So there you have it. Simple link cloaking with pretty URLs.

If you have any trouble getting this working, post in the comments. Also, I’ve tested this on a couple 2.2 versions of WordPress, and not at all on 2.3 or anything earlier. Have that backup of .htaccess handy!

3 Replies to “DIY Link Cloaking, With Pretty URLs”

Leave a Reply