Prevent website directory listing with .htaccess

What is directory listing?

There are those embarassing moments in life when you feel naked when someone sees at you something they aren’t suppose to see. The same happens with a site, when someone gets to a directory where there is no “index.php” or “index.html”. When those files are missing, the site shows the visitor the directory listing with all the files that are inside it.

For example, if we type “” in the browser, if the settings are not right, the browser will show the listing of the files that can be found in that directory. For security reasons, it’s better not to show the files inside the directory of the web server.

Most of the web servers are configured in such a way that they don’t show the listing of the directory. And yet, there are web servers that are not configured that way.

How can I prevent directory listing?

There is a simple way to prevent directory listing. Just put the any one of following code (after which make sure you press an Enter, the file must end with an empty line) into the .htaccess file.

[javascript src=””]


[javascript src=””]

If you place the .htaccess file in the root folder you will prevent the directory listing aswell the directory listings of the sub-folders.

What if I want to prevent the listing of certain extensions?

Let’s suppose that you don’t want to list “.jpg , .gif and .zip” and you don’t care about listing other files of that directory then you can out the following code inside .htaccess.

You will have to write:

[javascript src=””]

If your server is setup with preventing directory listing then you can add the following code in the .htaccess file to allow the directory listing.

[javascript src=””]

Leave a Reply

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

No spam? * Time limit is exhausted. Please reload CAPTCHA.