Control directory indexes with an .htaccess file

 

Overview

This article explains how to manage the directory indexing feature at DreamHost, which is enabled by default. This feature allows you to control what visitors see when they view your website.

Background

Directory indexing allows visitors to view a list of files instead of the actual webpage. When directory indexing is enabled, visitors can look inside any directory that does not have an index.html (or index.php) file in it and browse its contents. This is probably not the behavior you typically want, especially if the directory contains script configuration files, which can pose a security risk.

See this article to learn more about how to create an .htaccess file on your web server

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

How to manage directory indexes

You can control how directory indexes function on your website by making various modifications to the .htaccess file.

If your web server was recently upgraded to Ubuntu 18 (Bionic), your site may display a Coming Soon page that does not exist on your web server. To fix this, you can add the following line to your site's .htaccess:

DirectoryIndex disabled

Turning off directory indexes

To disable directory indexes throughout your entire site, place the following line within an .htaccess file in your domain's folder:

Options -Indexes

When a visitor tries to browse the contents of any directory of your site, a 403 (Forbidden) error appears.

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 visitors to browse, simply create a new .htaccess file in that particular folder and add the following:

DirectoryIndex disabled
Options +Indexes

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

Using 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

Index files guidelines

Make sure you are aware of the following when working with alternative index files:

  • 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 shows up even if you have an index.php file in the same directory).
  • You can change your own DirectoryIndex setting to anything you choose 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 displays a 404 forbidden error.

Changing the index file

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 you wish to use as the index file. When you visit your site, the pagename.html file displays, not your index.html file.

If the DirectoryIndex is missing

If you specify something like example.html as 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.

Using a 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 and then index.html. If neither is found, it displays /example.php.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?