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.

ErrorDocument 404 /error_file/404.html
ErrorDocument 401 /error_ file /401.html
ErrorDocument 404 /error_ file /404.html
ErrorDocument 500 /error_ file /500.html

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.

php_flag html_errors on
php_flag display_startup_errors on
php_flag display_errors on
php_flag  log_errors on

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.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

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

order deny,allow	

deny from <ip>       // to deny one particular IP address
deny from all              // to deny all

allow from <ip>     // to allow one particular IP address
allow from all            // to allow all

also to deny all and allow one

order deny, allow
deny from all
allow from <ip> 

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 :

<filesMatch "\.(htaccess|htpasswd|ini|phps|log|sh)$">
 Order Allow,Deny
 Deny from all
</filesMatch>

To Password Protect your Directories/Files:

AuthType Basic
AuthName "Sorry, Restricted Area"
AuthUserFile /path/to/protected/directory/.htpasswd
Require valid-user

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)

John : johnpswd
Alex : 4A8A08F09D37B73795649038408B5F33

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:

<FilesMatch "\.(mp3|mov|pdf|jpg)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

Or

 
AddType application/octet-stream .mp3
AddType application/octet-stream .mov
AddType application/octet-stream .pdf
AddType application/octet-stream .jpg

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

Similar Posts

Leave a Reply

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