Alex Little Alex Little - 4 months ago 21
Apache Configuration Question

Apache2 AddDescription match exact file/directory name

I'm trying to add a description to the (apache generated) directory listing. However I can't figure out a way to get the description text to only be applied for an exact match of the file/directory name.

For example, if I have the following directory/file structure

phpinfo2.php
phpinfo/
phpinfo.php
another_file.php


and I apply the directive:

AddDescription "PHP" phpinfo


This matches 3 of the 4 files/directories, as follows:

phpinfo2.php PHP
phpinfo/ PHP
phpinfo.php PHP
another_file.php


However what I'd really like is:

phpinfo2.php
phpinfo/ PHP
phpinfo.php
another_file.php


as I only want the AddDescription directive to match the exact file/directory name.

The docs (http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html#adddescription) say that "File is a file extension, partial filename, wild-card expression or full filename for files to describe." - so it's not possible to only match a full file/directory name?

Any help much appreciated, or if there may be alternative ways to achieve what I'd like. Thanks.

Answer

You can do this:

AddDescription " " *?phpinfo
AddDescription " " phpinfo?*
AddDescription "PHP" phpinfo

Apache applies the first matched rule and skips any following rule. The trick here is to use " ", which is the default content for a file/folder that doesn't match anything, to nullify any file/folder that contains phpinfo but isn't exactly phpinfo, so that only a file/folder exactly named phpinfo gets the description applied.

Note that you cannot use AddDescription "" ... because Apache does not allow an empty string parameter and will throw an error.

ref: Apache 2.4 doc