Work At Home Mom

How To Cloak Affiliate Links Using Htaccess

You’ve undoubtedly seen the super long affiliate links. For a variety of reason these can be less than ideal. For one, long url’s can break in an email and in a best case scenario, people may just not click because they recognize it as an affiliate link. Here’s how to cloak your affiliate links using .htaccess

So, besides the broken links in emails and people not clicking because they look affiliate links, why else would I consider cloaking affiliate links?

The best reason of all is that you maintain full control over those links and where they redirect to. With a url shortener service like tinyurl, you are at their mercy. Should they decide to close the doors one day or sell out to some business who wants to charge a fee, or even worse, redirecting a portion of your links without your knowledge.

You also have the ability to quickly change where your links redirect to at any time, this applies to line by line redirects and not a broad, entire merchant redirect. I’ll explain more about those.

Let me start by saying that you must be on an apache server, .htaccess does not work on Windows servers.

I’ll cover a couple different ways that I like to cloak affiliate links.

1. Redirection for an entire merchant using variables.

Rather than have a hundred links like

send link A to Site A
send link B to Site B

We can tell the server to send everything that comes into folder ”recommends” to Site A which in this case is Clickbank.

RewriteEngine On
RewriteBase /recommends
RewriteRule ^(.*) http://AFFILIATE.$ [L]

You can download this code in a text file here, replace AFFILIATEwith your clickbank id. Create a folder named “recommends”, change the name of your htaccess to .htaccess and make sure there is not .txt extension, upload it into the newly created “recommends” folder.

You are now ready to start setting up your cloaked affiliate links. From now on, instead of linking directly to the merchant, you will link to the “recommends” folder on your own site and just add the publisher to the end of your link, the htaccess with take the publisher, insert it into the $1 position and off they go.

If you’re not quite sure what the PUBLISHER is, just go the the Clickbank Marketplace, choose which product you wish to promote and click the “create hoplink” link. You will be given a link that looks like this.

The bolded blue text is the publisher, so your actual link would look like this…

NOTE: the “recommends” folder is just an example. I use the folder “go” among others., you could name the folder anything you like, “more”, “details”, “coupon”, “discount”. Just change the “recommends” in the .htaccess to match and change your links accordingly.

Counting Your Cloaked Link Clicks

The downside of cloaking your links with htaccess is you won’t know how many times your links are getting clicked on. Well, it took me a bit but I found a way to not only count those clicks but also to log ip’s. If you know of a better method, please leave a comment below, I’d love to hear about it.

Here’s how to do it

Download Click Counter Deluxe (it’s a free click counter) and install it. If you need a little help with unzipping, uploading files, etc.., you can probably find some free video demonstrations at Webmastering 101 Video Guide. Just spend a few minutes watching some of the relevant videos if you need to.

Now that you’ve installed the click counter, we need to modify our .htaccess file to send our clicks through the script before redirecting. If you installed the click counter in the default directory, your new .htaccess will look like this…

RewriteEngine On
RewriteBase /recommends
RewriteRule ^(.*) /ccd/index.php?xrl=http://AFFILIATE.$ [L]

Download this code in a text file here.

You can now log in to your click counter admin area and monitor your clicks.

Now, Let’s Do Amazon Links

For this, we’ll assume you are going to create a folder named “amz”, you can change it to whatever you like however, just change the folder name in the .htaccess file and create your links accordingly. Once you’ve created your “amz” folder, download the text .htaccess file for amazon here and edit it by adding your clickbank affiliate id, rename it to .htaccess and upload into your “amz” folder (or whatever folder you created for this). Make sure the amazon url is all on one line, it may wrap here due to width constraints.

RewriteEngine On
RewriteBase /amz
RewriteRule ^(.*)$1?ie=UTF8&tag=AFFILIATE-ID&linkCode=as2&camp=1789&creative=9325&creativeASIN=$1 [L]

In the code above, you will see two $1, these will be replaced by the ASIN when you link to the folder like so…

No more going through the Amazon affiliate area to create product links, just go directly through Amazon, when you find a product you wish to promote, click through to the detail page and find the ASIN and then create your link.

You will find the products ASIN under the Product Details section on the product page.

Product Details
Product Dimensions: 3.7 x 1.6 x 2.5 inches ; 6.2 ounces
Shipping Weight: 3 pounds (View shipping rates and policies)
Shipping: Currently, item can be shipped only within the U.S.
Item model number: A590IS

Using the above as an example which is for a Digital Camera, our link would look like

If you want these counted, just add


into your amazon .htaccess file as it is in the clickbank .htaccess file. It will send the clicks through your counter script before redirecting.

That’s It For Today

I think that’s a good start for today. I will pick up where we left off here in a future post and we’ll do one .htaccess file for a multitude of individual links, not a broad, merchant based redirect like we did here.

Also, there are many, many other things we can use .htaccess and mod rewrite for that will help in your online business and we’ll get into more of those as well so be sure and come back.

Take Care,


Additional Articles From "Technical"

30 Responses to “How To Insert Adsense Into WordPress Post”

  1. I’ve tried this code on a theme or two where it didn’t work. The fix for that is a little more complicated because it generally has something to do with your CSS. If you are comfortable with css, you can look in there for the problem (copy and paste entire css file into a text file first in case you have problems, you can just restore to default). I’ll post complete details in a different post for solving this in css.

  2. Home Care Dublin on January 6th, 2009 at 4:36 am

    Just one comment – since adsence seems to be another of google’s great tools – although we haven’t tried it on our blog yet, we have recorded a huge success with google adwords.

    So thanks for this, we might look at this in future and then use your advice.

  3. Thanks Gail. I’ve heard that your position for the Adsense is best. i.e. in the top left hand corner because it simulates a nav bar? Geoff D.

  4. Geoff, Where you place your adsense ads kind of depends on what your page is about. Is that your primary source of revenue for the page? You may want that to be their first click or a second chance to make money should they not buy from you.

    Here’s what Google says Where should I place Google ads on my pages?, and there are some free videos here Webmaster 101 Video Guide that pertain to Adsense Monetization.

  5. There is another 3 ways that you can applied to insert Google Adsense code at the TOP LEFT & TOP RIGHTS of any blog post.

    Based on my experienced and my research, this adsense ads unit in Top Left or Top Right corner have highest CTR. Adding this ads unit/s is very simple and only requires a bit code before and after your Google Adsense code.

  6. Ben Anderson WT on February 25th, 2009 at 12:57 pm

    I’ll try this out and see if this works for me. Thanks.

  7. hi, i am completley confused as to how i am meant to put the code onto my wordpress blog webiste, please help!

  8. Hi Adam,

    Sorry you are having a problem. I will put together a short video when I get a chance. Meanwhile, where are you getting stuck at? I’ll try and help you.

  9. Hi, This is a code that is supposed to place the 250 X 250 square ad in WordPress blog of Prosense Grey template. It doesn’t work. Maybe you can take a look and tell me what is wrong. Help.

  10. Thanks for the great tip.
    Can you tell me the best way to ad adsense to the page rather than the post?
    I am using an Adsense ready blog at present but would like to change to another which is not adsense ready.
    Hope you can advise.

  11. Ken, if you are using the ProSense theme as I am here, just paste in < ?php include('adsense_singlepost_top_square.php') ?> directly above < \?php the_content('

    Read the rest of this entry »‘); ?> (verify the adsense_singlepost_top_square.php is the 250×250 ad and replace if necessary being sure not to remove the div tag, < \div id="adsense_singlepost_top_square"\>. It should be but I can’t remember if the default for that was 250×250 or if I changed it.)

    If you have any problems with it, let me know.

    Edit: sorry, I had to add some \ so that it wouldn’t mess this post up.

  12. Hi Jack. You can manually ad the code anywhere you like. Just go to your theme editor in your WP admin and paste the code where you would like it to display. single.php is the actual posts, page.php is for any pages you create. You can also edit archives.php, index.php and pretty much all of them.

    Just copy everything from these files before you make any changes, that way if something doesn’t work, you can just paste back in to restore to original.

    Pasting the code in single.php directly below < \div id="content"\> should place it at the top of the page like you see on this page. (edit: had to add \ to the div tag above because it messed the post up)

    Pasting adsense code right above < ?php endwhile; else: ?> should place it under the post, like you see on this page.

    Alternatively, you can create seperate text files for each ad size, upload them in your themes folder and include them in the page by pasting < ?php include('adsense_250x250.php') ?> into the template files instead of the entire code.

    You might also find something useful in this post,

    If you have any trouble, let me know and I’ll be happy to help any way I can.

  13. I tried to send this and first I got an error message, and then when I tried to send it again, I got a duplicate send message. So will try again.

    If I go in “Appearance” and then to “Editor” in WordPress, and go to the right side “theme files” I find “single post (single.php) but below that there are several adsense_singlepost_entries and the one that I sent you previously was from adsense_singlepost_top-square.php. Is that the one I replace?

    I am still a little confused about the exact entry. If I take the entry from your web page and just put in my adsense code where you have it in Red, and I don’t add or delete anything else, Is that the exact entry that I need?

    Thanks, Ken

  14. Ken, I think you about got it!

    If you’re using prosense,just paste…

    <?php include(‘adsense_singlepost_top_square.php’) ?>

    into single.php directly above <?php the_content(’Read the rest of this entry »’); ?>

    Check adsense_singlepost_top_square.php and make sure that it looks like this…

    <div id=”adsense_singlepost_top_square”>

    Then check your stylesheet, style.css for…

    #adsense_singlepost_top_square {
    float: left;
    margin-right: 10px;

    If it’s not there, add it.

    Let me know!

  15. Ken, you can download the requested files here

    If you still have trouble, reply with a link to download yours and I’ll look at your single.php and the adsense code page (I won’t publish the comment).

  16. Big Pumpkin on May 1st, 2009 at 10:08 pm

    Hi Gail,

    Are you able to insert the adsense code into some individual posts but not all? I can do it site wide i.e. every single post has adsense, but I want adsense only on some of my posts. Thanks in advance :-)

  17. Hi Big Pumpkin,

    The only way I can think to do that is to create a new category for the posts you don’t want the adsense on and use a different template for that or those categories. I do this on some sites to rotate different types of banners according to the category.

    I’ve been meaning to write up a simplified post on how to do that but haven’t gotten to it yet. Meanwhile, you can get some more info on that here, and here,

    Good luck

  18. Big Pumpkin on May 2nd, 2009 at 3:11 am

    Hi Gail,

    Thanks for reverting so quickly. I read the two links you suggested but it means nothing to me – LOL. I am a total IT retard :-) I’m just going to wait for you to write your ‘simplified’ post on this topic, because I understood your other posts. Thanks again!

  19. Big Pumpkin, you talked me into it!

    How to Set Up Single Post and Category Specific Templates in WordPress

    It’s not so easy to “simplify” something so complex, hehe, but I tried!!

    Hope that helps!

  20. Tried the insert however all that came up on my page was a 302/252 empty space with my adsense pub number in it


    I assumed that it was my adsense id pub number that went in there

    I am new to all this coding and may have got it wrong

    Be glad of any redirections as would love to have this google ad systen in this spot as everyone recommends it as the winning place on the post or page

    Regards Lennie382

  21. Hi lennie382, you need to paste the entire google adsense code in that spot and not just pub-2286xxxxxxxxxxxx

    Let me know if you have any more problems.

    There’s another way if that doesn’t do it for you posted a few comments up…

  22. Hi Gail,

    I’m a total nubie in wordpress. Do you by any chance have a ready to download prosense template that has an adsense ad block in the top left corner similar to yours? Have tried things discussed above and can’t really make that ad block appear! Just a thought… I suppose this will be a very big help to a lot of your subscribers.. Thanks!


  23. Hi Edwin, the files you are requesting can be found here.

    If you have any trouble with it, let me know and I’ll be glad to help.

  24. Hello,

    so simple, so easy, so useful !!!


  25. Thanks for this nice solution. Was banging my head with this problem since 2 days. Actually, i was using the plugin Adsense now, it makes it very simple. But it doesnot put it that neatly. Thanks

  26. Thanks. Right on point! Everyone else was complicating the issue. and I don’t like using plugins for this b/c I’ve noticed the developers will program it to include their adsense to show up on your blog. So I’d much rather go this route. Thanks again!!!

  27. I’ve been looking for this plugin and finally found it. Thank you so much!

  28. I’ve tried it couple of times on my blog but it always shows on top of post but not within it, any ideas why ? Thanks

  29. Thanks man,

    You have refreshed my memory enough for me not to have to use any plugins…

    Wow.. I had forgotten how easy it was to edit WP :)

    You are like a star in heaven – people who simply offer free advice.. free good advice on the Net are the true keapers of the Net :)


  30. If you’re using the php include method in the 11th post from the top, make sure you’ve added the following to your css file

    #adsense_singlepost_top_square {
    float: left;
    margin-right: 10px;

    and that your adsense_singlepost_top_square code starts with

    < div id="adsense_singlepost_top_square" >

    and ends with

    < /div >

    (remove the spaces before and after the < and > . Had to add the space or it wouldn’t show here.)

Leave a Reply

free forex expert advisor