Disable directory listing with .htaccess .htaccess

If your website is hosted on an Apache web server, then by default, if someone navigates to a directory on your website, they will see a page listing the contents of the directory with links to each file. This may present a security risk as anyone can see all of your files in that directory just by visiting it in their browser. The easiest way to disable automatic index generation/directory listing across your whole website is with .htaccess.

Directory listing

How to prevent directory listing with .htaccess:

1. Create a file in the root directory of your website and name it ".htaccess"

Make sure to include the dot at the beginning of the file name.

If you just want to apply the changes to a single directory, you can place an .htaccess file in that directory, however you'll most likely want the changes to apply to the whole website, so to do that, place your .htaccess file in the root directory of your website.

2. Write or paste the following line of code into your .htaccess file:

Options -Indexes

The options directive determines which features are available in a certain directory.
-Indexes disables automatic index generation/directory listing.

Learn more about the Options directive on apache.org

3. Save the file and make sure it is uploaded

Save and upload your .htaccess file to your web hosting.

4. Refresh the browser

When you refresh the page in your browser, you should now see "Forbidden" instead of the files listed in the directory. This means that people will no longer be able to see the list of files in your website's folders when they visit them in their browser.

Forbidden

How to prevent directory listing without .htaccess

If you can't use the .htaccess file method, you can create an index.html file in all of your directories. This tells web servers to show that file instead of a list of the files in the directory. You would need to place a file named "index.html" or "index.php" or similar into every folder where you want to hide the directory listing. This can be a completely empty file if you want to show a blank screen or you could add a message or link in the file to show visitors.

Thanks for reading.
Ryan

Published on 3 Jun 2022
Last modified on 4 Jun 2022