Popular Uses of .htaccess File

.htaccess file is something that all web developers might be familiar with. But certainly many of those are not fully aware with its usage, confused with obfuscated terms used inside the .htaccess file and lack knowledge about many other benefits they can take from it.

.htaccess file is usually an ASCII or UTF-8 encoded file which contains special configuration statements/codes to control the behavior of services that are provided by the Apache web server to the website.

How to Use .htaccess File:

Displaying Errors:

The following lines in your .htaccess file make displaying the common web server errors like 404, 401, 404, 500 etc.errors with some sort of definitive description, which may help you better track the cause of error and thus to reach for its precise solution.

Where error_fie/XXX.html if the file,containing the error descriptive information in a more elaborative manner. These are the files placed somewhere in your domain, so put here the real domain specific path.

PHP Error Handling:

In some cases, error_reporting(E_ALL); ini_set(‘display_errors’, 1); may not give you proper error results, In that case you may use following code in your .htaccess file.

You can also turn them off by suffixing off at the end


php_flag html_errors off

Redirects to www Domain:

To automatically open website without www to with www prefixed domain name, you may include the code below into .htaccess file.

Also it escapes any duplication of domain name for web crawlers.

To prohibit some IP address (s) to Access you Website:

.htaccess file is a very useful way to deny, allow or restrict some IP address(s) to access your website

To Deny Access to php.ini File:

If you do not want anyone to access your php.ini and other essential files for security reasons, then you can use following code :

To Password Protect your Directories/Files:

If one tries to access the protected directory, this will prompt for username and password in form for a pop up with a header containing the Line “Sorry, Restricted Area”.

You need to place above code in .htaccess file and ,also have to create another file .htpasswd which will contain the list of usernames and passwords(in plain or md5 hash format)

Please make sure that both files. .htaccess and .htpasswd are placed in the same directory, that you need to protect.

To Force Allow Downloading of Media Files:

It happens many a times when you click a link associated with some media file like pdf, mp3, etc. but you want to download only and not to play automatically. Then you can add following code in your .htaccess file:


You can add more media types of your choice like .avi, .mpg, .wmv etc.

There are many more uses of .htaccess file, which I will try to explain in my coming tutorials. Thanks

Manish Kumar

Leave a Reply

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