Apache: How to Disable Directory Browsing

Updated: January 22, 2024 By: Guest Contributor Post a comment

Introduction

When configuring a web server, it’s crucial to ensure that your setup not only serves content efficiently but also securely. One aspect of security that can often be overlooked is directory browsing. By default, the Apache web server may list the contents of a directory if no index file is present. This behavior, while sometimes useful, can lead to unintended information disclosure about your server’s directory structure, configuration files, or other sensitive information. In this tutorial, we will discuss the necessary steps to prevent directory browsing in Apache.

Prerequisites

Before you begin, you should have the following:

  • An Apache web server up and running.
  • Access to the server with administrative privileges.
  • Basic knowledge of terminal usage and the Apache configuration files.

Understanding Apache Configuration Files

The Apache web server is configured by placing directives in plain text files. These directives are separated into various sections, allowing you to control the behavior of the server on a global, per-directory, or per-file basis.

The primary Apache configuration file is usually named httpd.conf or apache2.conf, depending on your operating system. Additional configuration settings can also be placed in .htaccess files located within the directory structure of your website.

Method 1: Using httpd.conf or apache2.conf File

The most secure way to disable directory browsing is by editing the Apache configuration file directly.

Step 1: Edit the Configuration File

Open the main configuration file using a text editor with administrative rights. For example:

sudo nano /etc/apache2/apache2.conf

Alternatively, you may need to edit the httpd.conf file:

sudo nano /etc/httpd/httpd.conf

Step 2: Disable Directory Listings

Locate the section that begins with <Directory /var/www/> or a similar DocumentRoot path. Then find the line containing the Options directive and remove Indexes from the list of options.

<Directory /var/www/>
    Options Includes FollowSymLinks -Indexes
    AllowOverride None
    Require all granted
</Directory>

By removing Indexes, you’re telling Apache not to list directory contents. Ensure that the line doesn’t contain the Indexes option after any changes.

Step 3: Restart Apache

After making changes, you need to restart the Apache service for the changes to take effect.

sudo systemctl restart apache2

Or on other systems:

sudo service httpd restart

Method 2: Using .htaccess File

If you don’t have access to the main configuration files or prefer to use an .htaccess file for per-directory settings, you can disable directory browsing by adding or editing an .htaccess file in the root directory of your website.

Step 1: Create or Edit .htaccess

Using a text editor, open or create an .htaccess file in the directory you wish to protect:

sudo nano /var/www/html/.htaccess

Step 2: Add the Directive to Disable Indexes

In the .htaccess file, add the following line:

Options -Indexes

This instructs Apache to not list the contents of this directory.

Step 3: Save .htaccess File

Save the file and exit the editor. Ensure that the file permissions are set correctly, allowing the web server to read the file.

Testing the Configuration

After you have disabled directory browsing by one of the above methods, it’s important to test that the configuration works as expected. Attempt to access a directory on your web server without an index file. You should receive a ‘403 Forbidden’ error or be redirected to another page, instead of seeing the list of files.

Note: Apache configurations might vary depending on your specific server setup and operating system. Always backup configuration files before making changes.

Conclusion

Securing your Apache server by disabling directory browsing is a simple but effective measure to protect your web content from prying eyes. By following the instructions provided in this article, you can enhance the security of your website with just a few configurations. Remember to regularly review your server settings, as security best practices can evolve over time.