Using .htaccess file for affiliate link redirects 9

If you make changes to your own .htaccess file, you do so at your own risk. You should always keep a backup of any file you change and if you are unsure what you are doing, contact technical services of your hosting provider.  The following tutorial is for Linux based servers.


Your server will have a folder where you upload your site.  I am going to call this the “root” folder.  In the root folder you may have a file called .htaccess – if it does not exist, you should be able to create it manually.  Just open a text editor, and save an empty file as .htaccess

Note the naming of this.  There is a period at the beginning, then the word htaccess.  Some text editors may try to save this with a txt extension, but it should not have one. 


The .htaccess file allows you to enter processing commands that can change the way your site behaves.  e.g. you might password protect a folder, or ensure that all of the pages on your site resolve to a domain with or without the www. bit no matter what is typed into the browser.  You might want to use Server Side Includes in pages with .html extension and you can set this up using the .htaccess file.  I have only scratched the surface on the types of things you can do with this file, but you get the idea – it is a powerful tool to have.


You can actually find an .htaccess in any folder on your server.  If a folder contains one, it is because that folder has specific requirements that the .htaccess file can offer.   However, for the purpose of this tutorial, we are only concerned with the .htaccess file in your root folder.


What I want to look at here is how you can use the .htaccess file to setup and control affiliate links.  The benefits of this include:

  • affiliate links cloaked.
  • manage all affiliate links for a site from one file (e.g. if you want to switch an affiliate link to a different merchant and you have the link on hundreds of pages, you can make just one change to the htaccess file and the link is updated on all pages ).
  • URLs are more user-friendly.

Using Redirects for Affiliate Links


The idea here is that we want to create a URL to be used for a specific affiliate product.


Suppose you are selling a BluRay Disc Player from Amazon on our site.  To create the redirect you need to choose a “name” for the link.  Make it short, yet related to the product so you can easily find it in the future (you may end up with 100s of redirects in your htaccess file).  In this case you might choose “bluray”.


To create the redirect, simply add this line to your .htaccess file (which you can edit in any text  editor, though I would not use something like Word unless you know what you are doing – the file needs to be saved as plain text and Word can add extra stuff):




Replace [LINK NAME] with the name of your link,  and [AFFILIATE LINK] with the actual link you got from Amazon.  You will end up with something like this:


redirect /bluray


Now, when you want to link to the Bluray player on a page of your website, you link to:




Substitute [MY DOMAIN] with your domain name and [LINK NAME] with the redirect’s link name.  Here is an example:


When someone clicks this link, the web browser will go and look for a page (in fact it will look for an index page in the bluray folder) and under normal circumstances a page not found error would be returned.  However, the htaccess file informs your server that whenever someone tries to access the /bluray folder, they need to be redirected to:


Look at the structure of the redirect again:


redirect /bluray


Cloaking a Clickbank Affiliate Link


Imagine the Clickbank affiliate link:


In my .htaccess file I add a line to the end of this file:


redirect /greenwidget


When I link to this product on my site, I use the following link:


Now, imagine that this link is placed on 100 pages of your site.  You get an email from the merchant saying they are closing their affiliate product (or maybe a better product comes along).  Instead of manually changing the link on 100 pages of your site, you simply edit the htaccess file, and change the affiliate link that the “/greenwidget”  redirect points to.  Upload changes and all 100 links on your site are updated immediately.


Keeping affiliate links in one file like this allows you to make site-wide changes to links by just editing one file.


By naming your redirects with meaningful names, you can easily search for an affiliate link when you need to change it.


To end with a screenshot, here is one of my sites .htaccess file showing the redirects and a portion of code added by WordPress (since this is a WordPress site):



Leave a comment

Your email address will not be published. Required fields are marked *

9 thoughts on “Using .htaccess file for affiliate link redirects

  • Dan

    I have actually recently stumbled across this technique.

    Before I found out about this I almost avoided putting affiliate links within the text of articles cause I really didn’t want to go back and manage them if that program went away.

    So I had to use includes at the bottom of articles with less specific products so it would be easy to manage.

    Now that I have this technique I spread more affiliate links within the text of my articles and get more clickthroughs to merchants pages which also go to more specific products which helps make more money.

  • Jeff McCall


    Up until now I’ve actually been creating subdirectories with an index file that redirects.

    This is a much cleaner approach, a lot quicker to implement, and it’s going to make my server a lot neater without all those subdirectories.

    Many thanks for the tip.


  • Georjina

    Thank you Andy. This is the first time I’ve really understood how redirects work and it’s far more simple than I thought.

  • Skye Tyler

    What a great tutorial!

    This is such a easy and straightforward way to fix a gigantic 302 redirect problem I was having due to uncloaked affiiate links.

    Thank you, thank you, thank you.

    (And damn all those computer geeks with their stupid complex code and explanations I can’t understand)

  • Jes

    Thanks for the tip. I have close to 2000 EPN affiliate links on my site. That will be some big .htaccess file! Is there any trick for this? I don’t know, a second, third .htaccess file somehow linked?

  • Karen

    Can you help me, i have been reading your article with interest. I have a web site that has just been transferred to a new host and i have redesigned the site. The site was producing 2 to 3 leads a day not a lot but kept me going. My web guy put redirects in place in the access file and now i am getting no enquiries at all.

    I have mentioned the problem but he does not seem to think this is anything. My main keys words since developing the new wordpress site have actually gone up slightly, but alas no leads!!

    What i am asking is if my .htaccess file could be transferring leads elsewhere. Sorry to ask but you seem to be a mine of information on the htaccess file.

    Kind regards


    • Andy

      Yes, redirects can be used for a lot of things, and if someone else is controlling yours, or has been, you should check them. If you want to open a support ticket on my support site (, I’ll see if I can help. However, a quick check would be to click on the links on your site and see if they register in the affiliate/leads program.

      • Karen

        I think things are ok i have looked in the htaccess file all all redirects are fine and links all click through to my site;

        Thanks for your advice all interesting stuff because i never knew hacking could be so easy – frightening!