How to allow/deny/alter directory listing with .htaccess on your website

htaccess

Today I will show you how easy it is to allow or deny a visitor to your website to view a directory listing if a default webpage is not available. This is controlled in a single file called .htaccess. Continue reading if you want to know more.

Prerequisites

To begin, what I’ll be showing you requires the two following prerequisites:

  1. Your website must be on a server running Apache
  2. Apache needs to be setup to allow .htaccess overrides

Both of the above conditions need to exist, otherwise the following information will not work.

Editing .htaccess

In the root directory of your website, you need a file named .htaccess. If one currently exists, copy it to your local computer via FTP for editing. Otherwise, create one locally. Open the file in a simple text editor such as Notepad or VIM. Edit the file with one of the following lines as needed.

Disable Indexing
Options -Indexes

forbidden_directory_listing

Enable Indexing
Options +Indexes

fancy_directory_listing

Change Listing style

(This disables Fancy Directory listing.)

IndexOptions -FancyIndexing

non-fancy_directory_listing

Ignore files with specific extension
IndexIgnore *.zip *.txt

ignore_directory_listing

Seriously…it’s that easy. If you edit the .htaccess file in your root directory, then all sub-directories are affected. However, you can customize a specific subdirectory by creating and adding a new .htaccess file to that specific directory.

If you want to know more about .htaccess files, please visit the Apache.org website.