SEO Tips & Tricks

Hide & Shorten Affiliate Links using .htaccess or PHP code

Affiliate marketing is a major source of income for many high traffic blogs and websites and this is how the system generally works: Your site visitor clicks on an affiliate link or banner >> Go to the website (original or third party) of offered product or service >> Buys the product or service >> You (the webmaster) gets a small amount of commission for referring the customer to the other website.

These type of links are generally long, ugly and hard to remember. In this post we will learn how to hide and shorten them?

Very Important Notes:

  • If you have created a website which contains lots and lots of affiliate links and don’t have a valuable content to offer, then Google may either give your website (and its sub-pages) very low ranking in search results OR may penalize it completely. You should limit the total number of affiliate links on your web pages and try to add more valuable content to them.
  • No matter what other people says, you should always add a NOFOLLOW tag (rel=”nofollow”) to your direct affiliate and cloaked links.

Suppose your site’s home page URL is and you want to hide following affiliate link:

so that it looks like a part of your website’s address: OR

This can be easily done either by using a .htaccess file or a simple PHP code.

Using .htaccess

If you are using cPanel and want to make links a part of your primary domain, then the .htaccess file for it will be present in /public_html/.htaccess. And for making links a part of your sub-domain or an add-on domain, the .htaccess file for them will be present in /public_HTML/<sub-domain’s or add-on domain’s folder>/.htaccess.

You are required to edit this file and add following line in it:

Redirect 301 /<Term you want to use after the trailing slash> Full affiliate link. E.g. Redirect 301 /products

You can also make use of cPanel’s inbuilt redirection feature instead of manually editing the .htaccess file:

cpanel redirect

Just go to Redirects under Domains and select the domain from the drop down menu for which you want to create a redirection (Use 301 permanent redirect instead of 302 temporary redirect).

Now enter the term you want to use after the trailing slash and enter the full affiliate URL in the Redirects to field. Also make sure that Redirect with or without www. is selected for www. redirection.

Using PHP

Create a products.php file using a text editor like Notepad, Notepad++ etc. and insert following line of code in it:

<?php header(“location: <Your Affiliate Link Here>“); ?>. E.g. <?php header(“location:“); ?>

Now upload this file to the root directory of your domain, so that anyone can easily access it using

Once the link is hidden and shortened, you can also use it with a banner image using following HTML code:

<a href=”<Shortened Link>“><img src=”<Banner URL>“></a>. E.g. <a href=”“><img src=”“></a> OR <a href=”“><img src=”“></a>


  • Instead of placing the above PHP file in the root directory of your domain, you can also place it in a sub-directory say /external/, so that the address becomes
  • You may also want add Disallow: /external/ in your robots.txt file, so that search engine bots don’t crawl this directory.