DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 12.3 Generating Directory/Folder Listings

Problem

You want to see a directory listing when a directory is requested.

Solution

Turn on Options Indexes for the directory in question:

<Directory /www/htdocs/images>
    Options +Indexes
</Directory>

Discussion

When a URL maps to a directory or folder in the filesystem, Apache will respond to the request in one of three ways:

  • If mod_dir is part of the server configuration, and the mapped directory is within the scope of a DirectoryIndex directive, and the server can find one of the files identified in that directive, then the file will be used to generate the response.

  • If mod_autoindex is part of the server configuration and the mapped directory is within the scope of an Options directive that has enabled the Indexes keyword, then the server will construct a directory listing at runtime and supply it as the response.

  • The server will return a 404 (Resource Not Found) status.

Enabling directory listings

The real keys to enabling the server's ability to automatically generate a listing of files in a directory are the inclusion of mod_autoindex in the configuration and the Indexes keyword to the Options directive. This can be done either as an absolute form, as in:

Options FollowSymLinks Indexes

or in a selective or relative form such as:

Options -ExecCGI +Indexes

Enabling directory listings should be done with caution. Because of the scope inheritance mechanism, directories farther down the tree will also be affected; and because the server will apply the sequence of rules listed at the beginning of this section in an effort to provide some sort of response, a single missing file can result in the inadvertent exposure of your filesystem's contents.

Disabling directory indexing below an enabled directory

There are essentially two ways to work around this issue and ensure that the indexing applies only to the single directory:

  • Add an Options -Indexes to .htaccess files in each subdirectory.

  • Add an Options -Indexes to a <Directory> container that matches all the subdirectories.

For example, to permit directory indexes for directory /usr/local/htdocs/archives but not any subdirectories, use:

<Directory /usr/local/htdocs/archives>
    Options +Indexes
</Directory>

<Directory /usr/local/htdocs/archives/*>
    Options -Indexes
</Directory>

See Also

    [ Team LiB ] Previous Section Next Section