Apache 2 Custom Directory Listing

Greg Schenzel

You may wish to customize Apache directory listings so they look closer to the websites that you host. This page will show you how. You will set up a header and a footer page for each website (or server-wide). The page fragments can be standard HTML or SSI (requires mod_include). SSI (shtml) is recommended so that you can customize the header per-request. Remember not to double-define IndexOptions, ReadmeName, or HeaderName in your config.

Server Wide config

This example sets a custom directory listing for all server requests. SuppressHTMLPreamble is absolutely required in IndexOptions; do not discard. Example requires mod_autoindex, mod_include (for shtml).

<IfModule mod_autoindex.c>
    IndexOptions FancyIndexing IgnoreCase VersionSort SuppressHTMLPreamble
    ReadmeName /webdata/footer.shtml
    HeaderName /webdata/header.shtml
    Alias /webdata /var/www/somewhere

Multi-Domain config

A more realistic example involves a web server that serves multiple domains. You may wish to have custom directory listings taylored to each website. The following example will respond with custom pages for unixdev.net and netizenweb.com and supplies a default page for all other requests. This method is especially useful if you use mod_vhost_alias and do not have <VirtualHost>s defined for each website; otherwise you could manually "Alias /webdata /path" under each hosts directive instead of using mod_rewrite. If you intend to add/subtract RewriteCond/RewriteRule pairs for other domains, be sure to adjust the skip value [S=] accordingly. Example requires mod_autoindex, mod_include (for shtml), mod_rewrite.

<IfModule mod_autoindex.c>
    IndexOptions FancyIndexing IgnoreCase VersionSort SuppressHTMLPreamble
    ReadmeName /webdata/footer.shtml
    HeaderName /webdata/header.shtml

RewriteEngine on
RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 1

RewriteCond %{SERVER_NAME} unixdev.net
RewriteRule ^/webdata/(.*) /webdata-udev/$1 [PT,S=2]

RewriteCond %{SERVER_NAME} netizenweb.com
RewriteRule ^/webdata/(.*) /webdata-nweb/$1 [PT,S=1]

RewriteRule ^/webdata/(.*) /webdata-default/$1 [PT]

Alias /webdata-nweb /fs/files/www/uweb/root
Alias /webdata-udev /fs/files/www/vhost
Alias /webdata-default      /fs/files/www/root

header.shtml example

  <title><!--#echo var="SERVER_NAME" -->:<!--#echo var="REQUEST_URI" --></title>
  <h1>Index of <!--#echo var="REQUEST_URI" --></h1>

footer.shtml example


Example Sites

GNOME Icons for Apache