TL;DR
Prevent people from seeing a list of files and folders on your website by adding options to your .htaccess file. This stops unwanted access to sensitive content.
How to Block Directory Listing in .htaccess
- Understand the Problem: By default, some web servers will show a list of files and folders if there’s no index file (like
index.htmlorindex.php) present in a directory. This can expose important information.- This is often called ‘directory listing’.
- The
Options Indexesdirective controls this behaviour.
- Access Your .htaccess File: You’ll need to edit the
.htaccessfile in your website’s root directory (or the specific directory you want to protect). This is usually done via an FTP client or a file manager provided by your web hosting provider.- Important: Make sure your file manager shows hidden files.
.htaccessfiles start with a dot, so they’re often hidden by default.
- Important: Make sure your file manager shows hidden files.
- Edit the .htaccess File: Open the
.htaccessfile in a text editor.- If the file doesn’t exist, create it.
- Add or Modify the Options Directive: Add or modify the following line to disable directory listing:
Options -IndexesThis tells the server not to show a list of files and folders if no index file is found.
- Alternative: Prevent Listing for Specific Directories If you only want to block directory listing in certain directories, place the
Options -Indexesline inside a<Directory>block. For example:<Directory /path/to/your/directory/ Options -Indexes </Directory>Replace
/path/to/your/directory/with the actual path to the directory you want to protect. - Add a Redirect (Optional): To make it even more secure, redirect users who try to access a directory without an index file to another page. For example, your homepage:
Redirect 301 /path/to/your/directory/ /Replace
/path/to/your/directory/with the actual path and/with the URL of your homepage. - Save and Test: Save the changes to your
.htaccessfile.- Clear your browser cache.
- Try accessing a directory without an index file. You should now see a 403 Forbidden error or be redirected, instead of a list of files.
Important Considerations
- Server Configuration: Some servers might ignore the
.htaccessfile. If this happens, you’ll need to contact your web hosting provider for assistance. - Permissions: Ensure that your directory permissions are set correctly to prevent unauthorized access.
- cyber security: Blocking directory listing is a basic cyber security measure. It’s important to implement other security practices as well, such as strong passwords and regular software updates.

