How to Use of .htaccess File

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

What is .htaccess File:

.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 webserver 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 the 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

e.g.

php_flag html_errors off

Redirects to www Domain:

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

Also, it escapes any duplication of a domain names for web crawlers.

To prohibit some IP address (s) to Access your 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 the form for a pop-up with a header containing the Line “Sorry, Restricted Area”.

You need to place the 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 time 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 the following code in your .htaccess file:

Or

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

Recommended Posts For You

About Manish Kumar

I am PHP web developer and blogger.

View all posts by Manish Kumar

Leave a Comment

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