Create XML from directory list with Apache

In Apache world, the index of a directory can come from one of two sources – a file written by the user, typically called index.html or a listing generated by the server. If you enable automatic index generation (with Options +Indexes), Apache will create a HTML directory list for the URL without index page. With a few lines in httpd.conf, directory list can become a XML document. Let’s see how to configure Apache …

Open /etc/httpd/conf/httpd.conf file to add following directives. Please notice absolute path to the header file (path begins with a slash) because header.html will be globally used. In this example, header.html is saved to the document root.

# if is necessary, you can hide parent directory
IndexIgnore ..

# the XHTML keyword forces mod_autoindex to emit XHTML 1.0 code instead of HTML 3.2
IndexOptions XHTML

# force mod_autoindex to use only header.html when creates listing header
IndexOptions SuppressHTMLPreamble

# add HTTP header text/xml
IndexOptions Type=text/xml

# define absolute path to the header file
HeaderName /header.html

The default end of the index listing is: </body></html>, so if you want to have well formed XML document, header.html file should contain XML declaration and opened html and body tags.

<?xml version="1.0"?>
<html><body>

After httpd.conf is changed, and header.html is saved to the document root, restart httpd server. You should get the following XML output from the URL with images and no index page.

<?xml version="1.0"?>
<html>
  <body>
    <ul>
      <li><a href="00027.jpg"> 00027.jpg</a></li>
      <li><a href="00054.jpg"> 00054.jpg</a></li>
      <li><a href="00108.jpg"> 00108.jpg</a></li>
      <li><a href="00162.jpg"> 00162.jpg</a></li>
      <li><a href="00216.jpg"> 00216.jpg</a></li>
      <li><a href="00270.jpg"> 00270.jpg</a></li>
    </ul>
  </body>
</html>

This post describes only few features of automatic index generation. All details can be found on Apache mod_autoindex manual page.

2 thoughts on “Create XML from directory list with Apache”

Leave a Comment