.htaccess Tips and Tricks

October 07, 2006
Apache is one of the most popular web server which is stable and which scales very well. This web server which has been released under a free licence has the honor of powering many of the very high traffic sites, not to speak of the 100s of 1000s of ordinary websites dotting the net landscape.

Usually, when you host a website on a shared server, you are not provided any rights or access to modify the main configuration file (http.conf or apache2.conf depending on the OS on which the webserver runs) of the apache webserver. So you are severely constrained about changing the parameters of the web server (called server directives) and have to rely exclusively on the systems administrator manning the remote server.

This is where the .htaccess file has its use. By creating this hidden file in the root folder (or any sub folder) of your website, it is possible to set/unset almost all the server directives that can be set in the apache main configuration file. And these changes will take effect only for the root folder in which you created the file and its sub folders. Thus .htaccess file plays an important role in providing fine-grained control to an individual managing a website without giving blanket control of the web server.

For example, you can effectively use .htaccess file to deny access to everyone for certain folders residing withing your website directory. Or you can password protect your directory and allow access only to authorised people. But one of the most powerful and popular trick is to rewrite a particular URL using regular expressions. This can be useful if for instance, you have relocated some of the web content to another location thus changing the path of the file. So instead of giving a visitor an error page, you can write a couple of lines in a .htaccess file which will redirect the user to the new location of the web page.

Today, I came across this two part series (Part I and Part II) titled '.htaccess tips and tricks' which explains many of the tasks one can achieve using the .htaccess file which makes an informative read.