What you should know about .htaccess

What’s a .htaccess file?

Also known as hypertext access, .htaccess file, when placed in a particular server directory, is used to customize and configure certain request to that particular directory and its sub content (directories and web files).

In layman term, .htaccess is a flexible configuration file (normally placed at server root folder) that is often used to improve website navigation and viewing experience. .htaccess file is used most frequently when a server is shared among multiple users – for instance, a server used for shared hosting plans. .htaccess file on shared hosting plan allows users to alter their site configuration (such as rewriting URLs and making redirection) without affecting others (as they are unable to access server root folder).

Usage of .htaccess file

The following are the most common uses of .htaccess file.

Redirections

Ever heard of 301 redirect or 302 redirect – these web functions are done in .htaccess. 301 and 302 redirection are especially useful when you are moving your websites or taking an old files off from your website. For example, redirect http://www.webhostingsecretrevealed.com/old-reviews to http://www.webhostingsecretrevealed.com/new-reviews – not only your users will be taken to a designated page when they visit the old-reviews, search engine bots will also follow the redirection and thus credited the new page with its old page rank (Google PR).

Mod Rewrite

A mod-rewrite is done whenever one wish to rewrite a web address (URL). For example you can use mod-rewrite to modify a bad URL structure (for example, URL with too many parameters – too much of $ and & symbols). Mod-rewrite is also often used to switch a non-WWW URL to WWW; example, from http://webhostingsecretrevealed.com switch to http://www.webhostingsecretrevealed.com.

Such URL modification is necessary as it improves users experience and avoid unnecessary technical problems with search engine. For example, by switching URL likehttp://www.example.com/rdgrt1?hym%+mobile%games to http://www,site.com/mobile-games, it shows a shorter URL to your users (hence easier to remember) and improve search engine’s crawl rate of your site.

Customization of the error responses

You can also use .htaccess show custom error responses instead of just a ‘404 Not Found’ or ‘403 Forbidden’ page. This improves your website image as well as users viewing experience.

Other common .htaccess usage

Some other .htaccess usage commonly used by web developers:

  • Helps the server to control user agent caching to reduce bandwidth usage.
  • Used for users authorization and authentication, restrict users access to certain directories on server.
  • To specifies the security restrictions and information for the particular directory.
  • To avoid irresponsible webmaster hot-linking images hosted at your server.

Sample of .htaccess codes

Some sample codes on .htaccess

Mod rewrite

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Redirection

Redirect 301 /home http://www.example.com/
Redirect 301 /old-filehttp://www.example.com/new-file/

Setting directory index file

DirectoryIndex index.html index.php /index.php

File extension mapping

AddHandler php5-cgi .php .inc

Customizing error documents

ErrorDocument 404 /oops-file-not-found/
ErrorDocument 500 /oops-data-base-error/

Anti image hot-linking

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(subdomain.)?askapache.com/.*$ [NC]
RewriteRule ^.*.(bmp|tif|gif|jpg|jpeg|jpe|png)$ – [F]

Final words on .htaccess

While .htaccess offers incredible flexibility in configuring your websites; one should note that mismanaging or misusing of .htaccess can result in performance loss as well as creating security loop holes. If you are not knowledgeable enough, make sure you ask for advice and comment from the experts before making any changes on .htaccess files.

Add Comment