Create XML from directory list with Apache

More...

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.

This entry was posted on February 21, 2009 and is filed under Apache

Related posts

2 Responses to Create XML from directory list with Apache

  1. Manu says:

    Thanks a lot, just what I needed!

  2. dbunic says:

    @Manu - Thanks and I'm glad it was helpful. Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

In case of posting HTML tags or JavaScript code please convert special characters to HTML entities.
Especially pay attention to convert "<" character to "&lt;" entity!