Home | Best Of | Roadmap | Recommended | Free Stuff | IM Store | Web 2.0 | Article Directory List

Cloaking Links: How To Cloak Links For Free

Written by Mark Mason on May 30th, 2008 | Posted in Blogging, Web Monitization | 16 Comments

Tags: , , , , , , ,

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.masonworld.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:

  1. edit your .htaccess file
  2. 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

Popularity: 20% [?]

Related posts

16 Responses to “Cloaking Links: How To Cloak Links For Free”

  1. Mark Sierra at MeAndMyDrum.com (12 comments.) Says:

    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?

  2. Mark Mason (151 comments.) Says:

    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

  3. Forest Parks (7 comments.) Says:

    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.

  4. philip townsend (1 comments.) Says:

    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.

  5. Mark Mason (151 comments.) Says:

    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

  6. Gaje Master (3 comments.) Says:

    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.

  7. Mark Mason (151 comments.) Says:

    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

  8. Gaje Master (3 comments.) Says:

    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.

  9. Mark Sierra at MeAndMyDrum.com (9 comments.) Says:

    That’s why I always tell people that Mark rocks! ;)

  10. Gaje Master (3 comments.) Says:

    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?

  11. Mark Mason (151 comments.) Says:

    @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

  12. Dennis Guten (3 comments.) Says:

    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 to http://www.findvintagepostcard.com/reccomends/ed
    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.

  13. Mark Mason (151 comments.) Says:

    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.

  14. Mark Mason (151 comments.) Says:

    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

  15. Dennis Guten (3 comments.) Says:

    Mark,

    Thanks for emailing me that file. Everything works fine now.

    Dennis

  16. Mark Mason (151 comments.) Says:

    Great news. Glad I could be of service.

Leave a Reply