How To Make Apache Load Index.PHP By Default?

Published September 17, 2024

Problem: Apache Not Loading Index.PHP by Default

Apache web server doesn't load index.php files first when you open a directory. It usually looks for index.html first, which can be a problem for PHP-based websites. You often need to change settings to make PHP files load first.

Configuring Apache to Recognize index.php

Using .htaccess Method

To make Apache load index.php by default, you can use the .htaccess method. This involves creating a .htaccess file in your web root directory and adding a DirectoryIndex directive.

  1. Create a .htaccess file in your web root directory if it doesn't exist.

  2. Open the .htaccess file in a text editor.

  3. Add this line to the file:

    DirectoryIndex index.php
  4. Save and close the file.

This configuration tells Apache to look for index.php first when accessing a directory.

Multiple File Types

You can specify multiple file types in the DirectoryIndex directive. For example:

DirectoryIndex index.php index.html index.htm

This tells Apache to look for index.php first, then index.html, and finally index.htm.

Modifying Apache Configuration File

Another method is to modify the Apache configuration file directly. This applies the changes to all directories on your server.

  1. Find the Apache configuration file, usually named httpd.conf. Its location depends on your operating system and Apache installation.

  2. Open the httpd.conf file in a text editor with admin rights.

  3. Find the DirectoryIndex directive. It might look like this:

    DirectoryIndex index.html index.htm
  4. Update the line to include index.php at the start:

    DirectoryIndex index.php index.html index.htm
  5. Save the changes and close the file.

  6. Restart the Apache server for the changes to work. The command to restart Apache varies by operating system, but it's often:

    sudo service apache2 restart

    or

    sudo systemctl restart httpd

After using either of these methods, Apache should now recognize and load index.php files by default when accessing a directory.