Changing the default directory index via .htaccess

Learn how to change the default directory index page using an .htaccess file.

This article describes how to use the DirectoryIndex directive in an .htaccess file to specify which page is displayed when a visitor requests a directory.

Changing the default directory index page

By default on hosting.com servers, when visitors go to a URL that requests a directory, such as http://example.com/products, the web server looks for a file in the products directory named index.html. If the index.html file does not exist, the web server looks for index.htm and other similar filenames. If none of these files are found, the web server either lists the files in the current directory (if directory index listings are enabled), or displays a "403 Forbidden" error message (if directory index listings are disabled).

You can use the DirectoryIndex directive in an .htaccess file to specify a custom file or files that the web server looks for when a visitor requests a directory. To enable the DirectoryIndex directive, use a text editor to modify the .htaccess file as follows. Replace filename with the file that you want to display whenever a user requests the directory:

DirectoryIndex filename

You can also specify multiple filenames, and the web server will search for each file until it finds a match. Consider this example directive:

DirectoryIndex index.php index.html index.htm

In this directive, when a visitor requests the directory name, the web server looks first for an index.php file. If it does not find an index.php file, it looks for an index.html file, and so on until it finds a match or runs out of files to search.

📘

Note

If you want the directory index page setting to apply to your entire web site, add the DirectoryIndex directive to the .htaccess file in the publichtml directory. All subdirectories beneath it that do not have their own .htaccess_ file will inherit the setting.

More Information

For more information about the DirectoryIndex directive, please visit http://httpd.apache.org/docs/current/mod/mod_dir.html#directoryindex.

Related Articles