Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

What you're looking for, is mod_autoindex

http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html

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
share|improve this answer
    
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. 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. 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. Aug 19 '12 at 23:36
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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