On Monday I wrote about several reasons to cloak links. Basically, I argued that cloaking links for the right reasons (managing link destinations and click tracking) meant that link cloaking was a great idea.
Of course, the idea that link cloaking is useful is supported by the number of link cloaking software solutions out there. While you can certainly go out and purchase a link cloaking solution, you can do it for free. In this article, I show you how to cloak your links for free.
How Cloak Links
Basically, the trick to link cloaking is to use your .htaccess file to redirect links targeted at a special subdirectory to a simple php program. That php program parses the link text to grab a keyword, and redirects the link based on that. For example, in the link “http://www.latenightim.com/recommends/google”, the word “recommends” represents the special subdirectory and the word google is the keyword that will determine the actual destination link.
To implement this strategy, you need to complete 2 simple steps:
- edit your .htaccess file
- create a simple php program called cloak.php in you web directory.
Editing .htaccess
The .htaccess file is a file at the Apache web server reads to determine things like access permissions. You can also use this tile to “rewrite” incoming URLs. In our case, we will use the “modrewrite” function of apache to change our URL.
Edit your .htacces and add the following code near the top of the file:
# cloak affiliate links
RewriteRule ^recommends/([^./]+)$ cloak.php?req=$1 [L]
This code says to take all incoming URLs containing the word recommends, strip off and save the end of the URL, and pass that value to a program called cloak.php. In our above google example, this directive sees the word “recommends” in the URL and strips off “google”, passing the word “google” to cloak.php.
Redirecting URLs via PHP
"<?php" <== without the quotes
$request_id = $_GET [‘req'];
$myFile = “/your-www-directory/cloak.log”;
$refData = $_SERVER[‘HTTP_REFERER'];
$fh = fopen($myFile, ‘a') or die(“can't open log file”);
$stringData = date(“m/d/y”).”,”.$request_id.”,”.$refData.”n”;
fwrite($fh, $stringData );
fclose($fh);
if (strcasecmp ($request_id, “search”) == 0)
header( ‘Location: http://www.google.com/' ) ;
if (strcasecmp ($request_id, “find”) == 0)
header( ‘Location: http://www.yahoo.com/' ) ;
?>
The above code takes the request ID (“google” in our example) and tests it against possible matches “search” and “find”. Each time you want to add a new redirect, just add another if statement.
There are definitely sexier ways to write this code, but this is simple and easier to maintain.
You will note that cloak.php makes a not of all the redirects ir performs in cloak.log. We'll discuss that in the next post.
By the way, make sure that cloak.php has world execute permission, as apache usually has really limited permission to execute files.
(If you have questions about getting this to work, please post them in the comments. I'll do my best to respond).
Thanks,
Mark
Hmm, interesting approach. Hadn’t thought of it. I’ve just been creating a 1-line php file for each product I recommend. The file contains the true URL and I put it in a directory called “recommends”. Then I just link to that file and it redirects readers to the destination. Looks like the same amount of work, so when comparing it to your method, is there some advantage over the other, or would you say it’s just two ways of doing the same thing?
Thanks, Mark.
Well, I definitely like having all of the links in one file, and I like having the tracking code there. In the end, the result could be similar (assuming you can track with your one-line solution).
Regards,
Mark
I’m only just starting to see the importance of tracking individual links on my sites and actually have a few articles coming up where this may be very useful.
Sometimes it’s better to recommend a product that you believe in and not have you affiliate link shown so that no one thinks you are lying (if that makes sense!!).
Anyway this post has been stored in a safe place for future ref.
Hi
I am new to all this and have been looking to cloak my affiliate links on my website and to be honest i dont think you explain everyhting in laymans terms. I think you presume that people know more. I haven’t got a damn clue what you are talking about.
Hey Phillip.
Your comment made me smile.
I would love to help you get this running. Can you tell me where I lost you? Then we can start from there.
Thanks,
Mark
so, if i were to write an affiliate link such as http://affiliate name here/gajemaster.com I would need to put in recommends before gajemaster and it would cause it to not show gajemaster? Is this correct? I am a bit of a dummy in this area.
No problem. We all start at the beginning. And you are not a dummy if you are smart enough to ask the question.
Let’s say that you have a site that you are blogging on — like this one.
It’s url is http://www.masonworld.com
Now let’s say you want to promote a product that you believe in.
For example, I like Josh Spaulding’s Article Marketing Domination.
So, on my site, the URL for Article Marketing Domination is
http://www.masonworld.com/recommends/amd
In the cloak.php script, I have set up “amd” as an alias for the long affiliate link that you need to use if you want to get a commission selling Josh’s AMD report.
Does that help?
So, in your case, you might want to promote Blog Mastermind.
Instead of your current affiliate link of
http://www.blogmastermind.com/affiliates/index.php?af=820857
You could have something like this
http://gajemaster.com/recommends/blogmastermind
This gives you a clean, more pro look to your links. It also theoretically reduces the temptation for someone to see that you’re promoting an affiliate link and “steal” your commission. The idea is that you are less “in your face” with the link. I have no data to support this claim, but people do argue that.
Hope that helps.
Mark
You are pretty darn good at telling me like it is without making me sound like an idiot.lol. Thanks a heap this really got me to know what I needed to know.
oh, one more question for you. When I write my blog post do I use the affiliate link or do I type in recommends/affiliate name?
That’s why I always tell people that Mark rocks! 😉
@Mark — thanks man.
@Gaje — There are lots of really nice people in the IM niche. If anyone gives you attitude, you should run (do not walk) away.
So, once you get the script installed and working correctly, then I recommend that you always use it. That means that you always use the recommends/product URL in your post.
Just remember to test those links and make sure that you have done it correctly each time (don’t want to miss out on any commissions).
Regards,
Mark
I guess I am missing something here.
I put the line of code in my .htaccess file and uploaded
I created a directory called recommends in my site
I created the cloak.php file with the code you gave us, minus the quotes
I changed the ($request_id, “ed”) and I added a link on a page
When I click on the link I get this…
Parse error: syntax error, unexpected T_STRING in /homepages/28/d95249100/htdocs/findvintagepostcard/cloak.php on line 6
Am I doing this correctly Mark? Maybe it has something to do with…
By the way, make sure that cloak.php has world execute permission, as apache usually has really limited permission to execute files.
I’m not sure how to check this. I have a WordPress 2.6 site.
Mark,
Thanks for emailing me that file. Everything works fine now.
Dennis
Hey Dennis. Looks to me like the .htaccess part is working, but there is some problem with the cloak.php file. If you would like to email me your cloak.php file, I would be happy to take a look.
Hey Dennis. Got your email.
I see funny characters in your file — possibly from cut and paste from the blog. I also see that the path to the cloak.log file is not set in the file that you sent me.
I edited the file and I am emailing your a file that I think might work.
Regards,
Mark
Great news. Glad I could be of service.
Hey Mark,
thanks for the guide on the redirects, works beautifully, and the logging is definitely a plus!
I did however make some minor changes as follows:
For the .htaccess, I made the redirect go to the recommends directory for the cloak.php file, as I like to keep my root folder as clean as possible. It’s also worth noting that some people may need to include the instruction to turn on rewriting if it isnt already done in their file. So my .htaccess looks like:
(/.htaccess)
#You only the next line once, so if it’s in your file already don’t include it again
RewriteEngine On
# cloak affiliate links
RewriteRule ^recommends/([^./]+)$ recommends/cloak.php?req=$1 [L]
Thats the minor htaccess change done, now onto the cloak.php file, which now sits IN the ‘recommends’ directory along with the cloak.log file. I added some comments to your code so it was easier to see what was happening, and I also changed the date format to (YYYY-MM-DD HH:MM:SS) which now includes the time, and is in a format my database can play with at a later stage. My cloak.php now looks like this:
(/recommends/cloak.php)
<?php
$request_id = $_GET[‘req’]; //Get the request string
$myFile = “/PATH/TO/YOUR_SITE/public_html/recommends/cloak.log”; //Set the path to the log file
$refData = $_SERVER[‘HTTP_REFERER’]; //Get Referrer information
$fh = fopen($myFile, ‘a’) or die(“can’t open log file”); //Open the log file for writing
$stringData = date(“Y-m-d H:i:s”).”,”.$request_id.”,”.$refData.”n”; //Concatenate date, request string and referrer information
fwrite($fh, $stringData ); //Write the concatenated data to a new line of the log file
fclose($fh); //Close the file
/*—–Request String Redirections here—–*/
if (strcasecmp ($request_id, “markmason”) == 0) { header( ‘Location: http://www.masonworld.com/‘ );}
if (strcasecmp ($request_id, “google”) == 0) { header( ‘Location: http://www.google.com/‘ );}
if (strcasecmp ($request_id, “docs”) == 0) { header( ‘Location: http://www.google.com/docs‘ ); }
Thats it. Thanks again for the post, and I hope this may help anyone else struggling.
Regards
Vince
You’re absolutely right it’s time to cut it out…