GuidesApache Guide: Generating Fancy Directory Listings with mod_autoindex

Apache Guide: Generating Fancy Directory Listings with mod_autoindex

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




When a user requests a directory without specifying a particular
file name, Apache attempts to serve up a default document. The
document served is determined by the directive DirectoryIndex,
and is usually called index.html. However, if that document is
not present, it is sometimes desirable to instead give a complete
directory listing, permitting the user to select a file.

Sometimes you want to give users a wider selection of files to load. If you want to spiff up the default file listings that Apache returns when an index file is unavailable, you can generate fancy directory listings. Rich Bowen explains how.

Directory listings are generated by the module mod_autoindex,
which is compiled into Apache by default. Left to itself, it
generates an uninteresting file listing, but with the techniques
I’ll show you in this article, you’ll be able to make these
listings as fancy as you like.

If your directory does not have a index.html in it, they you
will get a directory listing, ordered by date, in which each
filename is linked to that file for download. For each file,
you are also given the last modified date of the file, and
the size of the file.

There are a variety of default icons defined, so that different
file types have different icons associated with them. And there’s
a link to the parent directory.

It’s actually pretty nice, but there are some annoying things about
it. The directories are interspersed amoung the files, rather than
appearing at the top like most of us are used to. There’s a
Description column, but there’s nothing actually in that
column. And although files are alphabetically listed, the a’s come
after the Z’s, rather than with the A’s.

Some of the problems can be solved with the directives available
in mod_autoindex.

For the whole story …

For the full scoop on what you can do with mod_autoindex, you
should see the documentation at
http://www.apache.org/docs/mod/mod_autoindex.html I’m working on
that while I work on this, so it should be a little more current
by the time you read this.

Changing the Icons

To the left of each file name in a file listing, there’s a little
icon which presumably represents the type of file you’re looking at.
There’s a pretty good selection of image files to choose from, but
there are some file types missing, and occasionally you might
want to have your own images, rather than the ones that ship with
Apache.

There are three directives with which you can rectify this
situation. These are the AddIcon, AddIconByType, and
AddIconByEncoding. They let you specify a particular icon
to be displayed, by file name, file type, or file encoding,
respectively.

AddIcon lets you specify particular file names that should
be displayed with a particular icon. The syntax of this directive
is:

     AddIcon (ALT,/url/of/image.gif) filename

ALT is the alternate text to be displayed if the image does
not load. filename can be a while file name, a wildcard, or some
portion of a file name, such as a file extension. Examples might be:

     AddIcon (Image,/icons/image3.gif) .gif .jpg .png
     AddIcon /images/tmp.jpg *~

AddIconByType is the preferred directive, as it lets you
specify an icon by mime type, rather than by file name. This
allows for matching entire categories of files, rather than having
to try to guess at file names.

     AddIconByType (Image,/icons/image1.gif) image/*

Finally, AddIconByEncoding lets you specify the icon by the mime
encoding of the file. The syntax is the same as the other
directives, and the argument is a mime-encoding:

     AddIconByEncoding /icons/compressed.gif x-compress

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories