What is .htaccess and How is it Uselful

What is .htaccess file and how is it useful ?

Specifically, .htaccess is the default file name of a special configuration file that provides a number of directives (commands) for controlling and configuring the Apache Web Server, and also to control and configure modules that can be built into the Apache installation, or included at run-time like mod_rewrite (for htaccess rewrite), mod_alias (for htaccess redirects), and mod_ssl (for controlling SSL connections).
A .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration. They are placed inside the web tree, and are able to override a subset of the server’s global configuration for the directory that they are in, and all sub-directories.

The original purpose of .htaccess””reflected in its name””was to allow per-directory access control, by for example requiring a password to access the content. Nowadays however, the .htaccess files can override many other configuration settings including content type and character set, CGI handlers, etc.

 

Myths :

For a common man, .htaccess may look like a file which has no name and “htacess” extension but the actual fact is : htaccess” is the name of a file which has no extension.

The . (dot) in its name suggests that it is a hidden file ( according to basic unix / linux architecture )

 

Usage :

URL Rewrites :

Servers often use .htaccess to rewrite long, overly comprehensive URLs to shorter and more memorable ones.
for example – Redirect to www (dynamically)
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]

Blocking and Allowing :

Use allow/deny to block users by IP address or domain. Also, use to block bad bots, rippers and referrers. Often used to restrict access by Search Engine spiders.
Order deny,allow
Deny from all
Allow from 23.57.228.211

Directory listing :

Control the output when directory is accessed directly without any file specification.
DirectoryIndex index.html index.php /index.php

Custom Error Documents :

Whenever any http error is thrown, set the page to be shown for that HTTP ERROR CODE.
ErrorDocument 204 /204.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

MIME types :

We can define MIME types in the .htaccess file, and make the server understand what kind/category of file is that.
AddType 'application/xhtml+xml; charset=UTF-8' .xhtml
AddType 'text/html; charset=UTF-8' .html
AddType application/x-shockwave-flash .swf
AddType video/x-flv .flv

Cache Control / Expiry Headers :

We can set cache control headers for what time a file is cached in user’s machine.
<FilesMatch "\.(flv|gif|jpg|jpeg|png|ico)$">
Header set Cache-Control "max-age=2592000"

OR
ExpiresActive On
ExpiresDefault A604800
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000

References :
  1. AskApache
  2. Wikipedia
  3. htaccess-Guide