How can I control my directory indexes with an .htaccess file?

Creating an .htaccess file on your DreamHost web server

View the following article for instructions on how to create an .htaccess file on your web server:

If the file already exists, view the following articles for instructions on how to update it (depending on if you're using an FTP client or SSH):

What is directory indexing?

Directory Indexing is when you visit your site and see a list of files instead of the actual webpage. If Directory Indexing is not enabled, visitors can look inside any directory that does not have an index.html (or index.php, etc.) file in it and browse the contents of the directory. This is probably not the behavior you typically want, especially if said directory contains something like script configuration files that could pose a security risk.

Directory Indexing is enabled by default at DreamHost. This allows you to further control what files control your site when visited.

Turning off directory indexes

Place the following line in an .htaccess file in your domain's folder to disable directory indexes throughout your entire site:

Options -Indexes

Now, if someone tries to browse the contents of any directory of your site, they'll get a 403 (Forbidden) error.

If a subdirectory contains an index file (such as index.html), that directory will still load. This option only disables directories without index files.

Turning on directory indexes for specific folders

If there's a particular directory in your site that you do want people to be able to browse, simply create a new .htaccess file in that particular folder. Add the following:

Options +Indexes

This overrides the "global" setting and allows users to view the content of that directory.

Alternative index files

When a visitor accesses your website, the server checks the folder for an index file. Some examples of common index files are:

  • index.htm
  • index.html
  • index.php
  • index.cgi
  • index.pl

The supported index files depend on how the server is set up. DreamHost's servers are set to:

DirectoryIndex index.html index.shtml index.htm Index.html Index.htm Index.shtml default.htm Default.htm default.html Default.html default.shtml Default.shtml page1.html index.pl index.cgi index.php index.php3 index.phtml home.htm home.html home.shtml index.wml index.pcgi quickstart.html
  • As long as you name your "index" file any one of the above, it will automatically load.
  • When you add a new domain/subdomain to your account, DreamHost creates a quickstart.html file for you and places it in your directory which loads by default unless you delete it or add your own index file.
  • If you have two files with names from the list above, Apache shows the one that appears first (e.g., index.html will show up, even if you have an index.php file in the same directory).
  • You can change your own DirectoryIndex setting to be anything you like using an .htaccess file. If the server cannot find an index file, it tries to display an index of all the files within the current directory; however if this is disabled, the server ends up displaying a 404 forbidden error.

Using an .htaccess file, you can change the index file to something else other than any of the index defaults listed above. To do this, insert the following line into an .htaccess file:

DirectoryIndex pagename.html

Change pagename.html to the page that you wish to use as the index file. If you then visit your site, the pagename.html file is what displays, not your index.html file.

If you specify something like example.html to be the directoryindex and you have not uploaded this file to your web directory, Apache shows a listing of all files since it cannot find the one you specified. Be careful where you place this rule (or any rule), because it is recursive.

Fail safe directory listing

The following allows a specific file to load just in case there is no default index file already set up:

Options -Indexes
DirectoryIndex index.php index.html /example.php

When a visitor requests a directory, Apache searches for index.php, then index.html, and if neither are found it displays /example.php.

See also

Did this article answer your questions?

Article last updated .