Stop indexing files in a directory using .htaccess

How to stop indexing of files in a directory using .htaccess?

Sometimes we do not want to show all the files in a directory (which by default is a behavior of web servers).

That can be done using htaccess file.

Though there’s a simple solution to this, put an index.html file showing a decent message “Not authorized to access this”. But that will have little control over management.

What if we want to have more control and do it at the initial stage only?

Use .htaccess file.

Put this in your .htaccess file

IndexIgnore *

here * (asterisk) matches all the files so none would be displayed.

If you do not want to show .css or .conf or .php or .sh files only and let other files being shown, then,

IndexIgnore *.css
IndexIgnore *.php
IndexIgnore *.sh
IndexIgnore *.conf

or in a single line

IndexIgnore *.css *.php *.conf *.sh

This can be made to work for relative paths also, if you want to put all your htaccess statements in a single file at root level (recommended)

IndexIgnore /common-files/scripts/*

To make some file like “myfile.html” your default file (instead of index.html) which opens when a user enters a URL to access some directory, then use,

DirectoryIndex myfile.html

You can specify more than one file names, in case first is not found then immediate next one would be opened.

DirectoryIndex myfile_1.html myfile_2.html

On the contrary, if you want to show file index under a directory (opposite of stopping index) then use this,

Options +Indexes

Secure your files from accidental display to the users,

<Files ".htaccess">
    Order Deny,Allow
    Deny from all
    Allow from

Use regular expressions (using ~) to specify file names (to secure files other than .htaccess)

<Files ~ "\.(gif|jpe?g|png)$">
   Order Deny,Allow
   Deny from all
  Allow from


<FilesMatch "\.(gif|jpe?g|png)$">

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>