Enable/Disable directory listings using .htaccess
By default when accessing any directory which does not contain an index file (ex: http://yourdomain.com/images/) a directory listing will be shown displaying a list of the files in that folder. Simply adding a blank/ 0 byte text file named “index.html†will prevent this directory list from being displayed, however another way to disable directory listing is by adding the following line to the .htaccess file in your public_html folder of your web hosting account :
That’s it, with this line in place, folders which do not contain index files will no longer show the default directory list.
What’s especially nice about doing it this way is all sub-directories of that directory will also get their directory listings turned off. You could then turn on indexes for one of those sub-directories just by making a .htaccess file in that directory with:
If you want the directory contents to be listed, but only if they were HTML pages and not images. Adding the following line to the .htaccess file. IndexIgnore *.gif *.jpg This would return a list of all files not ending in .jpg or .gif, but would still list .txt, .html, etc .
- Code: Select all
Options -Indexes
That’s it, with this line in place, folders which do not contain index files will no longer show the default directory list.
What’s especially nice about doing it this way is all sub-directories of that directory will also get their directory listings turned off. You could then turn on indexes for one of those sub-directories just by making a .htaccess file in that directory with:
- Code: Select all
Options +indexes
If you want the directory contents to be listed, but only if they were HTML pages and not images. Adding the following line to the .htaccess file. IndexIgnore *.gif *.jpg This would return a list of all files not ending in .jpg or .gif, but would still list .txt, .html, etc .