Htaccess DirectoryIndex

The DirectoryIndex directive allow you to set the default page when a directory is accessed. When a user visits your website, the server looks for the default index file.

Some examples of common index files are:

  • index.htm
  • index.html
  • index.php
  • index.cgi
  • index.txt

If the directory has index.html, it will be loaded by default. So if a user visits then will be loaded

Syntax: DirectoryIndex file-name

Using .htaccess file, you can set the different index file name also. You can also set multiple files with the order you want to look for.

Example of DirectoryIndex :

Here is an example to set multiple index files using Htaccess DirectoryIndex.

DirectoryIndex index.php index.html home.html page.html

In this example, the server first checks if index.php exists. If PHP file not found, it will check for index.html, home.html and page.html next. Whatever file it will find first, it will be loaded.

Note: If you want same index page as a default for all subfolders in a website, set the DirectoryIndex directive to the .htaccess file in the public_html or root directory.

Fail safe directory listing:

We can set single default file for all subdirectories in a website if there is no default index file found in any subdirectory.

Options -Indexes
DirectoryIndex index.php index.html /site-default.php

In this example, if any subdirectory or folder does not have index.php and index.html, then /site-default.php will be loaded from the root folder.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.