.htaccess basic configuration example

In this article we will cover following topic of .htaccess file

  • Error Document
  • Disable Directory listing
  • Password protection to directory

Error Document

Just think of a situation, where your site is on error, but you are showing the default error message to Visitors. Error like, recently you have removed a file from server and that file is no more available on server. But users are trying to access that file, in the mean time server will show file not found error. But here our job is to show users a beautiful error message page so that user can properly understand.

This is even more useful where you want to create a log of such kind of errors for future analysis.


ErrorDocument errorCode Error Message file path


ErrorDocument 404 /error/404.html

List of error Codes

408 – Time out

404 – Bad file Request

403 – forbidden Access denied

401 – Unauthorized

500 – Internal Server Error

502 – Service temperately overloaded High Traffic

Disable Directory Listing

  • Disable all the file listing inside a directory

IndexIgnore *

  • Except rar file everthing will be listed

IndexIgnore *.rar

  • Except Image, every thing will be listed

IndexIgnore *.jpg *.jpeg *.png *.gif *.bmp

Password Protection to directory

Think of a situation where you want to add security. You want a solution where Site will ask for an Username and Password while accessing a directory.

To create such solution, You need two types of files

  • .htaccess
  • .htpasswd

Inside .htaccess file, we have to write following

AuthName “Site Authentication System”

AuthUserFile /.htpasswd
AuthType Basic
require valid-user

Inside .htpasswd, we have to write following

If you are using Unix or Linux system you can use the encryption system inside the password file. If you using windows OS then you have to use plain password inside Password file.


Keep Learning :)