How to Use .htaccess to Easily Mask Links

January 5, 2009

This guide assumes you have already setup a website running on an Apache based webserver. If you are using IIS you will need a third party plugin such as IIS Rewrite.

Often times you may want to redirect links from your site as opposed to linking directly. This has a few benefits:

  • If you need to change a link’s address you only have to change it in one spot instead of updating every page.
  • Many visitors distrust long URLs such as those used for affiliate marketing
  • Its easier to track how many people are clicking which links by using a redirect in between

First, create a special directory for the .htaccess redirect file. This is to prevent from interfering with any other .htaccess files that may be on your website. Name your directory something like links/ or visit/ as this is what the visitor will see in their status bar when they hover over the link.create a .htaccess file with the following

RewriteEngine On
 
#  Shopping
RewriteRule   ^amazon$  http://www.amazon.com
RewriteRule   ^ebay$  http://www.ebay.com
RewriteRule   ^newegg$  http://www.newegg.com
 
#  Search Engines
RewriteRule   ^google$  http://www.google.com
RewriteRule   ^yahoo$  http://www.yahoo.com
RewriteRule   ^msn$  https://www.msn.com
RewriteRule   ^ask$  http://www.ask.com

You can place a pound sign (#) to write comments and organize your links. Now, if you wanted to link to Google you would use the address http://www.yoursite.com/links/google (or whatever directory you created). Notice the (^) before and ($) after the link name.

Conclusion and Further Reading

.htaccess is a powerful way to manage your site. There are many more applications for .htaccess files such as password protection and migrating to a new file structure.

Look for a more comprehensive guide to .htaccess files here on EJ Web Development.

del.icio.us Digg Facebook Google reddit SlashDot StumbleUpon Technorati

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Comments are closed.