Skip to content

Directory listing on Apache — when to enable, when to disable

Control directory listing on Apache with Options Indexes — when an auto-index is useful, the IndexOptions tuning, and the security default that should be off.

Directory listing is one of those Apache defaults that often turns on by accident — an Options +Indexes deep in an Include, or Options All somewhere — and starts publishing the contents of /uploads or /backups to anyone who asks. This article is the audit, the disable-by-default policy, and the legitimate cases where directory listing is a feature (file mirrors, internal download pages) and how to tune it cleanly.

How to verify

Check what Options are set across the config tree and whether any vhost is exposing directory listing:

grep -rE 'Options.*Indexes|<IfModule.*autoindex' /etc/apache2/sites-enabled/ /etc/apache2/conf-enabled/ /etc/apache2/apache2.conf
curl -sI https://example.com/uploads/ | head -3
curl -s https://example.com/uploads/ | grep -i '<title>Index of'

If the curl returns HTML with <title>Index of /uploads</title>, directory listing is on. That title is mod_autoindex’s signature; it appears any time autoindex serves a directory.

What’s happening

When Apache receives a request for a URL that maps to a directory (e.g. /uploads/ for /var/www/example.com/public/uploads/), it checks the DirectoryIndex directive — typically index.html index.php. If a matching file exists, Apache serves it. If not, the behavior depends on the Options Indexes setting:

  • Options +Indexes (or Options Indexes in a fresh block) — mod_autoindex generates an HTML listing of the directory contents and serves it.
  • Options -Indexes — Apache returns 403 Forbidden.

The default in modern Apache packages (Ubuntu, Debian) is Options Indexes FollowSymLinks in the global <Directory /var/www> block. That means any new directory without an index file shows its contents publicly.

The audit-and-fix is simple: change the global default to Options -Indexes, then explicitly enable indexes only in directories where listing is intentional.

The procedure

  1. Audit the current global setting. In /etc/apache2/apache2.conf or /etc/apache2/conf-enabled/:

    grep -B1 -A3 '<Directory /var/www' /etc/apache2/apache2.conf

    The default block usually looks like:

    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
  2. Disable Indexes globally. Change to:

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

    Note the absence of Indexes. The -Indexes form also works:

    Options -Indexes +FollowSymLinks
  3. Disable autoindex module if not needed anywhere. If no directory on the box needs auto-listing:

    sudo a2dismod autoindex
    sudo apache2ctl configtest
    sudo systemctl reload apache2

    With the module gone, even an accidental Options +Indexes cannot serve a listing — mod_autoindex is the engine that builds the HTML.

  4. For directories where listing is a feature, enable it explicitly. For an internal download repository:

    <Directory /var/www/internal/downloads>
        Options +Indexes
        IndexOptions FancyIndexing HTMLTable VersionSort FoldersFirst NameWidth=40 SuppressDescription
        IndexIgnore .* *.tmp *~
        Require ip 10.0.0.0/8
    </Directory>

    IndexOptions controls the listing format. FancyIndexing adds icons and sortable columns. IndexIgnore hides dotfiles and temporary files. Require ip restricts access to internal networks.

  5. For public mirror sites, where directory listing is the entire point:

    <Directory /var/www/mirror>
        Options +Indexes
        IndexOptions FancyIndexing HTMLTable VersionSort FoldersFirst NameWidth=*
        AddDescription "Ubuntu LTS releases" releases
        HeaderName HEADER.html
        ReadmeName README.html
    </Directory>

    HeaderName and ReadmeName inject HTML before and after the listing.

  6. Reload and verify.

    sudo apache2ctl configtest
    sudo systemctl reload apache2
    curl -sI https://example.com/uploads/   # should be 403 now
    curl -sI https://example.com/internal/downloads/   # should still serve listing if VPN'd in
  7. Set up a CI check. A common drift is a developer dropping Options All in a .htaccess or new vhost. A grep in CI:

    grep -rE 'Options.*Indexes|Options All' /etc/apache2/ && exit 1 || exit 0

    Run this in your config repo’s pre-commit or CI.

Common pitfalls

  • A subdirectory Options +Indexes overrides the parent Options -Indexes — vhost-level discipline is necessary; setting it only at the global level is not enough.
  • Options All includes Indexes — using Options All anywhere is rarely correct.
  • Some packages (vendor-supplied web apps, control panels) ship .htaccess files with Options +Indexes — AllowOverride None defends against them; see .htaccess pitfalls.
  • A directory served by a front controller (WordPress, Laravel) does not need Indexes because the rewrite catches every request — disabling globally has no impact on app-served URLs.
  • An empty index.html is enough to suppress directory listing without changing Options — a quick mitigation for a single directory, but not a substitute for the global config.

Stack Harbor sets Options -Indexes globally on every Apache host we onboard, with explicit +Indexes only in documented exceptions. The audit is a one-time pass during onboarding, then a CI check defends against drift. The discipline is part of the Managed Operations hardening baseline alongside other small but compounding defaults.