How to use an .htaccess file to alter the default directory index page?

On HostGinger Hosting servers, by default, when users access a URL that requests a directory, such as http://xyz.com/products, the web server searches for a file called index.html in the products directory. If there is no index.html file, the web server looks for index.htm and other files with similar names. 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) if none of these files can be found.

To define a specific file or files that the web server looks for when a visitor requests a directory, use the DirectoryIndex directive in an .htaccess file. Make the following changes to the .htaccess file using a text editor to enable the DirectoryIndex directive.

Replace filename with the file that you want to display whenever a user requests the directory:

DirectoryIndex filename

Additionally, you can specify more than one filename, in which case the web server will check each one until it finds a match. Take the following directive as an example:

DirectoryIndex index.php index.html index.htm

With this directive, the web server first searches for an index.php file when a visitor requests the directory name. It searches for an index.php file first, then an index.html file, and so on until it finds a match or runs out of files to search.

Was this answer helpful?

Related Articles

How to change HTTP headers using .htaccess files

This article shows how to edit the HTTP headers that Apache sends to a client directly....

How to enable and disable directory index listings using an .htaccess file?

To enable index listings for a directory add the following line to the .htaccess file: Options...

How to protect a website folders using .htaccess

The .htaccess file is a configuration file used by Apache-based web servers. Directives in...

What is .htaccess files?

An .htaccess file is a plain-text configuration file that you can use to alter the settings of...

How to protect .htaccess file contents?

By default, everybody has access to an .htaccess file's contents. Due to the fact that it exposes...