Currently, I use a PHP script to list the contents of a directory that does not have an index file, but I don't want to have to copy it to every directory without an index.

Can Apache do it automatically?


What you're looking for, is mod_autoindex


Make sure you have it enabled in the Apache configuration:

LoadModule autoindex_module /usr/lib/apache2/modules/mod_autoindex.so

Note: you may have to change the path directory to the module, or even change the module name. On SOME Apache servers I've seen, it may be just:

LoadModule mod_autoindex /path/to/mod.so

In response to the comments: (edit)

#put this in a `.htaccess` in your root directory
#this sends 403 errors to any directory w/o index.
Options -Indexes
ErrorDocument 403 /path/to/file.php
  • Are you sure that this is really the one im looking for? Cuase im not able to find how exactly i will load the php file i created if there is no index file in the current directory. – Adonis K. Kakoulidis Aug 19 '12 at 22:59
  • @AKLP - If you are trying to list all the files in the directory that HAS NO INDEX then Apache does that automatically with that module. If you want to load "the PHP file" automatically, it just has to be called index.php but what does the PHP file do, list the directory contents? If so, delete the PHP file and use the mod_autoindex. – ionFish Aug 19 '12 at 23:07
  • What i want to do is load the php file, but not for a single one but all directories without an index file. – Adonis K. Kakoulidis Aug 19 '12 at 23:14
  • @AKLP - Ok, this is totally unorthodox, but here's a way maybe to do that. You need to specify a custom error page and then use my edits in my answer above to put in the Apache config. Note: the downside to this, is that if you need a "real" 403 error, it's not possible. Apache will send the PHP file you have ANY time a 403 is sent. – ionFish Aug 19 '12 at 23:34
  • Yes cool, that works. I want this feature mostly for localhost/dev work so i dont mind missing the 403 error thanks! – Adonis K. Kakoulidis Aug 19 '12 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.