Disable directory listing (Apache Linux)

Send Us a Sign! (Contact Us!)
This article has been published [fromdate]

If no DirectoryIndex (normally configured as index.html, index.htm, index.php, welcome.html) present in a directory, and if mod_autoindex is enabled (which by default is normally enabled), Apache would list the directory contents, as the following:

Disable directory listing (Apache Linux)

While useful for some cases, you would want to disable this feature (perhaps for security reason). There are few ways to do this.

Disable mod_autoindex

The easiest way is probably to disable the autoindex module entirely. This would affect all the sites hosted on the server, which could be both good and bad.

The module is called autoindex, and can be disabled by the a2dismod command, as in the following example:

sudo a2dismod autoindex

Directory's Options directive

The option could be applied per-directory, via the Directory's Option directive as the following:

<Directory /var/www/mysite>
    Options -Indexes
</Directory>

Notice that it's -Indexes and not +Indexes.

This could be set in your Apache's configuration file.

.htaccess

The above method would also work by adding a .htaccess file with the following content that you want directory listing to be disabled:

Options -Indexes

Restart Apache service for the changes to take effect.