Directory Listing Configuration
Here we describe various types of things that the server can send when you request a directory rather than an individual file.
Trailing Slash Redirection
If you request a directory without including the trailing slash in its name (i.e.
http://example.com/dir rather than
http://example.com/dir/), then Apache must send a redirect to add the trailing slash to the URL. This is necessary so relative hyperlinks will work in the resulting file.
For this to work, Apache must know the name of the server so that it can send a redirect back to itself. Normally, when [WWW] UseCanonicalName is set off, the name supplied by the client in the Host HTTP request header is used. If UseCanonicalName is on, then you need to assure that [WWW] ServerName is set correctly in order for this redirect to work.
Directory Indexes
When a directory is requested, Apache may be configured to send a particular file within that directory automatically. This is configured with the [WWW] DirectoryIndex directive. It can list one or more files that Apache should search for in the directory, with the first existing file being returned to the client. For example:
Code:
DirectoryIndex index.html index.htm index.php welcome.html
Directory ListingsIf no file from the DirectoryIndex directive can be located in the directory, then [WWW] mod_autoindex can generate a listing of the directory contents. This is turned on and off using the [WWW] Options directive. For example, to turn on directory listings for a particular directory, you can use
Code:
<Directory /usr/local/apache2/htdocs/listme>
Options +Indexes
</Directory>
To p
revent directory listings (for security purposes, for example), you should remove the Indexes keyword from every Options directive in your configuration file. Or to prevent them only for a single directory, you can use
Code:
<Directory /usr/local/apache2/htdocs/dontlistme>
Options -Indexes
</Directory>
Excluding FilesIf you would like listings to be enabled, but you want to omit particular files, you can use the [WWW] IndexIgnore directive. For example, to omit any filename starting with tmp and also the parent directory link (..), you could use
Code:
IndexIgnore tmp* ..
Headers and FootersThe directives [WWW] HeaderName and [WWW] ReadmeName configure a file to be included, respectively, above and below the file listing. If no path is given, Apache will look for these files in the directory being listed. For example:
Code:
HeaderName header.html
ReadmeName footer.html
A path starting in slash can be used if you want the same files included for all directories:
Code:
HeaderName /site/header.html
ReadmeName /site/footer.html
Styling the listingThe directory listing is highly configurable. The [WWW] IndexOptions directive gives lots of choices for different configurations and the [WWW] IndexStyleSheet directive allows a CSS stylesheet to be specified. A typical configuration might look like:
Code:
IndexOptions FancyIndexing HTMLTable
IndexStyleSheet /css/autoindex.css
Extended exampleFor a more complete example, including a configuration for the icons displayed with the files, see conf/extra/httpd-autoindex.conf as distributed with Apache httpd.
Occasionally, you may not have a default index document in a directory. If a default document is not found, whenever a visitor types in the directory name in their browser, a full listing of all the files in that directory will be displayed. This could be a security risk for your site.
To prevent without having to add a default index document to every folder, you can enter the following line in your .htaccess file to
Disable a directory's contents from being shown:
Code:
Options -Indexes
DirectoryIndex DirectiveDescription: List of resources to look for when the client requests a directory
Syntax: DirectoryIndex local-url [local-url] ...
Default: DirectoryIndex index.html
Context: server config, virtual host, directory, .htaccess
Override: Indexes
Status: Base
Module: mod_dir
The DirectoryIndex directive sets the list of resources to look for, when the client requests an index of the directory by specifying a / at the end of the directory name. Local-url is the (%-encoded) URL of a document on the server relative to the requested directory; it is usually the name of a file in the directory. Several URLs may be given, in which case the server will return the first one that it finds. If none of the resources exist and the Indexes option is set, the server will generate its own listing of the directory.
Example
Code:
DirectoryIndex index.html
then a request for
http://myserver/docs/ would return
http://myserver/docs/index.html if it exists, or would list the directory if it did not.
Note that the documents do not need to be relative to the directory;Code:
DirectoryIndex index.html index.txt /cgi-bin/index.pl
would cause the CGI script /cgi-bin/index.pl to be executed if neither index.html or index.txt existed in a directory.
Module mod_dir
This module provides for "trailing slash" redirects and serving directory index files.
Status: Base
Source File: mod_dir.c
Module Identifier: dir_module
Summary
The index of a directory can come from one of two sources:
[list][*]A file written by the user, typically called index.html. The DirectoryIndex directive sets the name of this file. This is controlled by mod_dir.
[*]Otherwise, a listing generated by the server. This is provided by mod_autoindex.
The two functions are separated so that you can completely remove (or replace) automatic index generation should you want to.
A "trailing slash" redirect is issued when the server receives a request for a URL
http://servername/foo/dirname where dirname is a directory. Directories require a trailing slash, so mod_dir issues a redirect to
http://servername/foo/dirname/.
Directives
DirectoryIndex