Directory listing exposes file names and directory structure when a URL maps to a directory that lacks a default index page, turning a minor misconfiguration into an information leak for backups, source files, and internal assets.

Apache selects a directory response using DirectoryIndex from mod_dir and generates a listing only when mod_autoindex is loaded and the directory permits Options Indexes with no matching index file present.

Commands and paths below assume Ubuntu or Debian with /etc/apache2/ layouts and the apache2 systemd unit, and the same controls apply elsewhere with different file locations and a httpd service name.

Adding more filenames to DirectoryIndex reduces accidental exposure, but a missing index file still triggers listing when Indexes is enabled.

Prefer Options -Indexes in a VirtualHost or <Directory> block for production, and reserve disabling mod_autoindex for servers where listings should never work anywhere.

For those using platforms like cPanel, there are platform-specific methods to disable Apache's directory listing.

Disable Apache directory listing by disabling autoindex module

Disabling mod_autoindex blocks auto-generated directory listings across all hosted sites, regardless of per-directory settings.

  1. Open a terminal with sudo privileges.
  2. Disable mod_autoindex with a2dismod.
    $ sudo a2dismod --force autoindex
    Module autoindex disabled.
    To activate the new configuration, you need to run:
      systemctl restart apache2

    Any URL that previously relied on an auto-generated listing will return an error after mod_autoindex is disabled.

    On platforms without a2dismod, comment out the LoadModule autoindex_module line in the main Apache configuration and restart the service.

  3. Validate the Apache configuration syntax.
    $ sudo apache2ctl configtest
    Syntax OK
  4. Restart the Apache service to unload the module.
    $ sudo systemctl restart apache2
  5. Request a directory URL without a default index file to confirm listing is blocked.
    $ curl -i https://example.com/downloads/
    HTTP/1.1 403 Forbidden
    ##### snipped #####

    Replace example.com and the path with a directory that has no DirectoryIndex file present.

Disable Apache directory listing via Directory's Options directive

Setting Options -Indexes is the most common hardening approach because it disables browsing for a specific directory while keeping mod_autoindex available for other sites.

  1. Open the relevant VirtualHost or global configuration file in a text editor.
    $ sudo vi /etc/apache2/sites-available/mysite.conf

    The change can be placed inside a VirtualHost file or in /etc/apache2/apache2.conf/ for a global default.

  2. Locate the <Directory> block that matches the web root path.
    <Directory /var/www/mysite>
        Options Indexes FollowSymLinks
    </Directory>
  3. Disable directory indexing by setting -Indexes in the Options line.
    <Directory /var/www/mysite>
        Options -Indexes +FollowSymLinks
    </Directory>

    Use relative + and - prefixes consistently when modifying Options values.

  4. Validate the Apache configuration syntax.
    $ sudo apache2ctl configtest
    Syntax OK
  5. Reload the Apache service to apply the configuration change.
    $ sudo systemctl reload apache2
  6. Request a directory URL without a default index file to confirm listing is blocked.
    $ curl -i https://example.com/mysite/
    HTTP/1.1 403 Forbidden
    ##### snipped #####

    A 403 Forbidden response is typical when directory listing is disabled and no index file exists.

Disable Apache directory listing using .htaccess

A .htaccess file can disable listings in a specific directory when server-wide configuration access is unavailable, but it only works when AllowOverride permits Options directives.

  1. Confirm the directory allows .htaccess overrides for Options.
    <Directory /var/www/mysite>
        AllowOverride Options
    </Directory>

    Enabling AllowOverride All increases per-request filesystem checks and can permit unsafe directives if the directory is writable by untrusted users.

  2. Create or edit the .htaccess file in the target directory.
    $ sudo vi /var/www/mysite/.htaccess
  3. Disable directory indexing inside .htaccess.
    Options -Indexes

    Changes to .htaccess apply on the next request when AllowOverride permits the directive.

    The .htaccess directive is ignored when AllowOverride does not permit Options.

  4. Request a directory URL without a default index file to confirm listing is blocked.
    $ curl -i https://example.com/mysite/
    HTTP/1.1 403 Forbidden
    ##### snipped #####
  5. Reload the Apache service after editing AllowOverride in server configuration.
    $ sudo systemctl reload apache2
Discuss the article:

Comment anonymously. Login not required.