Web Server Configuration: htaccess File

A .htaccess file is a short name for hypertext access. It is a directory-level configuration file used by Apache-based web servers and allows for decentralized management of web server configuration.

These files are able to override a subset of the server's global configuration for the directory that they are in, and all sub-directories. You can make configuration changes on a per-directory basis. The .htaccess file server settings applies to the directory in which it is placed and to all subdirectories.

Enabling htaccess

By default, .htaccess files are normally enabled. So you need not worry on this. To confirm further, you can ask your web hosting company about this. They will be more than happy to enable it for you.

Comments in htaccess file

Comments start with the # symbol. Comments are optional and used for reference or explaining the codes. 


Some of the common uses of the .htaccess file is the ability to password protect directories, ban users or allow users using IP addresses, stop directory listings, redirect users to another page or directory automatically, create and use custom error pages, change the way files with certain extensions are utilized, or even use a different file as the index file by specifying the file extension or specific file.

Custom error pages

You can redirect your visitors to a custom error page if they encounter some error. For example, suppose you want to redirect 404 error pages to custom file (say  filenotfound.html), then use following code

ErrorDocument 404 /filenotfound.html;

Different index file

You can use a different file as the main index file that visitors see when they come to your site. Usually visitors are presented with index.html or index.php, but if you want some special page that you want to use as your site's main page, then use

DirectoryIndex index.php3 index.php

If index.php3 is not in the directory, then index.php will be used. You can add more file names.

Password protect directory

When you use .htaccess to password protect a directory, your site visitors will encounter a popup dialog box that requires to enter a username and password. This code will password protect the directory (say yoursite), and any subdirectories under it.

AuthUserFile /path/to/yoursite/.htpasswd
AuthType Basic
AuthName "Password Protected"
Require valid-user

Create a file .htpasswd which will store username and password in encrypted form.

Redirection using htaccess

Permanent redirect 301 is the most common and useful. For example to redirect your entire website to any other domain (say example.com)

Redirect 301 / http://example.com

For Joomla Installation

Joomla comes with an htaccess.txt file, but it needs to be renamed to .htaccess in order to use it.

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive