How To Disable Directory Browsing In Apache?

Published August 2, 2024

Problem: Security Risk of Directory Browsing

Directory browsing in Apache can expose sensitive information by allowing visitors to view and access files and folders on a web server. This vulnerability can lead to unauthorized access to confidential data or potential exploitation of the server's file structure.

Solution: Disabling Directory Browsing

Method 1: Using .htaccess File

To disable directory browsing using an .htaccess file:

  1. Create a new .htaccess file or open an existing one in the root directory of your website.

  2. Add this line to the file:

    Options -Indexes
  3. Save the file and upload it to your web server.

  4. Put the .htaccess file in the directory you want to protect. For example, put it in the root directory to disable directory browsing for the entire website. To protect a specific folder, put it in that folder.

Tip: Verify .htaccess Implementation

After implementing the .htaccess file, test it by trying to access a directory without an index file. If directory browsing is properly disabled, you should see a "403 Forbidden" error instead of a list of files.

Method 2: Modifying Apache Configuration

To disable directory browsing by changing the Apache configuration:

  1. Find the Apache configuration file. It's usually named httpd.conf and located in /etc/httpd/conf/ or /etc/apache2/ depending on your system.

  2. Open the file using a text editor with admin rights.

  3. Find this line:

    Options Indexes FollowSymLinks
  4. Change it to:

    Options FollowSymLinks
  5. Save the file and close the text editor.

  6. Restart the Apache server to apply the changes. Use this command:

    sudo service httpd restart

    Or, depending on your system:

    sudo systemctl restart apache2

Both methods will disable directory browsing on your Apache server, improving your website's security by preventing unauthorized access to your file structure.