Web Server

Prevent Directory Listing using htaccess

When any user comes to the website and if your default page like index.html, index.php not found then webserver will list out all the directories and files to the webpage.

Here is the snippet which disable the directory listing to the web page. Below code will produce 403 error if no default page is found.

Disable Directory Listing

[cc lang=”apache”]
# Disable directory listing
Options -Indexes

Also we can disable specific files to be listed on the web page. Suppose if you don’t want to list out the .zip and .txt file.
Below code will not list .zip and .txt file and rest of all files will be listed.

Disable only .zip and .txt files Listing

[cc lang=”apache”]
# Prevent listing of zip and txt files
IndexIgnore *.zip *.txt

Note: We can setup the default file access using “DirectoryIndex” directive.


Leave a Reply

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