Disable Directory Listing Using .htaccess

htaccess_featured

Do you have a directory full of images or zip files that you do not want people to be able to browse through? Typically a server is setup to prevent directory listing, but sometimes they are not. If not, then you have to fix this yourself. Directory listing can compromise the security of your website, so it is very important that you control directory listing wisely.

There are two common ways of preventing directory listing:

  1. Placing a blank file named index.htm inside each directory that you would like to protect.
  2. Using .htaccess file.
As the first method is self explanatory and needs no more explanation, in this tutorial I will explain to you how to use a .htaccess file to disable directory listing.
The method is fairly easy. All you need is to start your notepad or any text editor you like and type the following code:
Options -Indexes

Save the file and then upload it to the root directory of your website, after you have uploaded the file to the correct location just rename it to “.htaccess” and that is it, now no body can brows through your web directories.

In some cases the directory listing is disabled by default and to enable it you can use the following code:

Options +Indexes

[box type=”warning”] Always be careful and do not put any security compromising files in this directory.[/box]

The code mentioned above affects all directories, but if you want to disable directory listing for a specific directory you have to use the following code and then place the .htaccess file inside the directory you intened to restrict access to.

IndexIgnore *

If you want users to be able to display all files inside a folder except images then you can use this:

IndexIgnore *.gif *.jpg

If you have any questions or would like me to discuss a certain topic please send me a message or simply just leave a comment below!

Disable Directory Listing Using .htaccess
4 votes, 5.00 avg. rating (96% score)
  • Posted in: Tutorials
Husamaldin Tayeh

Posted by

Husamaldin is a computer programmer, blogger, entrepreneur and an overall thinker. He is the founder and editor of Coders Mount. He loves everything about computers and programming. Find out more about him here or follow him on Twitter Google+ | LinkedIn.