Prevent Directory Listing using .htaccess

What is directory listing ?

As we know, most of the website contains “images” folder inside the root folder. If we type “” in the browser and it shows the listing of the files in the browser like the picture below which means that directory listing is allowed on that web server. It’s better not to show the files inside the directory of the web sever for the security purpose.

Why and what is prevent directory listing ?
Most of the web server are configured in such a way that it doesn’t show the listing of the directory but some of them are not.In some cases, you don’t want to allow users to view the files of the particular directry in such a direct way and prevent the listing of that directory.

How to prevent directory listing ?

Put the any one of following code into the .htaccess file

Options -Indexes
IndexIgnore *

If you place that .htaccess file in the root folder then direcotry listing is prevented of the sub-folder as well. And if you place the .htaccess file inside the “images” folder then directory listing are prevented of that particular directory only.

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

IndexIgnore *.gif *.jpg *.zip

Finally, 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.

Options +Indexes

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

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