Chapter 6. Handling directories

Table of Contents
6.1. The problem with directories
6.2. Using default documents
6.3. Automatic indexing of directories
6.3.1. Basic listings
6.3.2. Improving the listings
6.3.3. Using an HTML table
6.3.4. Summary of the auto-indexing module
6.4. Using both modules

Directory URLs. Some URLs (typically those that end in /) correspond to directories rather than plain files. We need to determine how to deal with these.

dir_module. The dir_module module provides the facility of using a default file (typically called index.html) in a directory on behalf of the directory itself.

autoindex_module. The autoindex_module module provides the facility of generating a listing of the files and subdirectories within a directory.

Combining approaches. We will also consider how to combine the two approaches so that an index.html file is used if present and a directory listing is used if not.

6.1. The problem with directories

A document has a MIME content type. We've already seen that the URLs that correspond to files can now be served with the correct MIME type, so long as the file's name has the appropriate suffix. But what about directories? In particular a "top level" URL such as http://www.dept.cam.ac.uk/ will trigger a request for /. There's no such file so the server can't serve it yet, let alone determine a MIME content type for it.

There are two solutions to this issue. The first, and simplest, is to nominate a filename (such as index.html) and instruct that if a directory is requested which contains a file with this name then that file should be treated as the target of the request. This is the approach we will follow in Section 6.2.

The other approach is to give a listing of the directory's contents, typically in HTML format. We will cover this in Section 6.3.