Directory indexing options using .htaccess file

Hi guys, in this post I am trying to describe various directory indexing options available through an htaccess file.

htaccess stands for “Hypertext Access” and according to Apache .htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular directory, and the directives apply to that directory, and all subdirectories thereof.

Most of the hosting companies enabled directory indexing in the server configuration file. If the directory indexing is enabled and you dont have a default directory file (index.html, index.php etc..) in the directory  then all the files in the directory get listed on the browser as in the following image

On the other hand if the directory indexing is disabled in the server and if you dont have a default file (index.html, index.php etc..) in the directory, you get a message like in following image on the browser and we can access one file by specifying its name.

If your server disabled directory indexing and you need directory indexing you can use the following directive in the htaccess file  to show the file list on the browser.

Options +Indexes

If the server enabled directory indexing and you don’t need directory indexing, you can use the following directive to disable the directory indexing.

Options -Indexes

It is also possible to hide files of specific format, for example the following directive hide all the gif and html files in the directory from the files list.

IndexIgnore *.gif *.html

You can also change the default directory file by an htaccess directive.

DirectoryIndex about.html

The above htaccess directive set about.html as the default file in the directory.

If you want to show more details in the index such as Last modified time, Size etc.. then you can use the fancy indexing directive in the htaccess file

IndexOptions FancyIndexing
or
IndexOptions +FancyIndexing

The above command in the htaccess file shows more details of the files in the index as the following image.

If FancyIndexing is enabled in the server and you want to remove fanyindexing use the following directive in the htaccess file

IndexOptions -FancyIndexing