ThisCalculator

Everyday

Change Apache Default Index File: Step-by-Step

Updated · Standard formula, shown below

Change Apache Default Index File: Step-by-Step

This guide shows you how to change the default index file that Apache serves when a directory is requested without a specific filename. Instead of always looking for index.html, you can make Apache use a different file, such as index.php, home.htm, or even a custom name like start.php. You’ll need access to your Apache configuration files and the ability to restart the Apache service.

What You’ll Need

  • Access to the Apache configuration files (usually httpd.conf or apache2.conf) or a virtual host configuration file.
  • Command-line access to restart Apache (e.g., sudo systemctl restart apache2 or sudo service httpd restart).
  • Basic familiarity with editing text files on your server.

Step-by-Step

  1. Locate the Apache configuration file. The main configuration file is often named httpd.conf (on RHEL/CentOS/Fedora) or apache2.conf (on Debian/Ubuntu). Virtual host files are usually in a directory like /etc/apache2/sites-available/ or /etc/httpd/conf.d/.
  2. Open the configuration file in a text editor. Use a command like sudo nano /etc/apache2/apache2.conf or sudo vi /etc/httpd/conf/httpd.conf. You need root privileges to edit these files.
  3. Find the DirectoryIndex directive. Search for the line that starts with DirectoryIndex. It is typically inside a <IfModule dir_module> block. If you don’t find it, you can add it yourself.
  4. Modify the directive. The DirectoryIndex directive sets a list of file names to look for, in order. For example, to make index.php the first choice, change the line to:
    DirectoryIndex index.php index.html
    This tells Apache to serve index.php if it exists, and fall back to index.html if not. You can list any number of files, and you can also use a wildcard like index.* if you want Apache to match any file starting with index..
  5. Save the file and exit the editor. In nano, press Ctrl+O to save and Ctrl+X to exit. In vi, press Esc, then type :wq and press Enter.
  6. Test the configuration (optional but recommended). Run apachectl configtest or sudo apache2ctl configtest to check for syntax errors. If you see Syntax OK, you’re good to go.
  7. Restart Apache. Use the appropriate command for your system:
    sudo systemctl restart apache2 (Debian/Ubuntu) or sudo systemctl restart httpd (RHEL/CentOS). You can also use sudo service apache2 restart or sudo service httpd restart.
  8. Verify the change. Visit your website’s root URL (e.g., http://example.com/). If you placed a file named index.php in the document root, it should now be served instead of index.html.

Common Mistakes

  • Forgetting to restart Apache. Changes to the configuration file only take effect after a restart or reload. If you don’t restart, the old settings remain active.
  • Editing the wrong file. If you have virtual hosts, the DirectoryIndex directive in the main config might be overridden by a directive inside a virtual host block. Make sure you edit the correct file that applies to your site.
  • Using a file that doesn’t exist. If you set DirectoryIndex start.php but start.php is not in the directory, Apache will return a 403 Forbidden or 404 Not Found error. Always ensure the file exists.
  • Incorrect syntax. A missing space or a typo in the directive can cause Apache to fail to start. Always run a config test after editing.

Tips

  • Use a fallback list. It’s wise to keep index.html in the list as a fallback, especially if you have static pages or if the primary file is temporarily missing.
  • Use wildcards carefully. DirectoryIndex index.* will match any file that starts with index. (e.g., index.php, index.htm), but the order of matching is based on the server’s file system order, which may not be predictable. It’s better to list specific names.
  • Override per directory. You can also place a .htaccess file in a specific directory to set a different DirectoryIndex for that directory only, without affecting the global configuration. For example, in .htaccess you can add: DirectoryIndex index.php.
  • Check file permissions. The web server user (e.g., www-data or apache) needs read permission on the index file. If the file is not readable, Apache will return an error.

Troubleshooting

If the change doesn’t work as expected, try the following:

  • Check the error log. Look at Apache’s error log (usually in /var/log/apache2/error.log or /var/log/httpd/error_log) for messages about the directory or file. It might tell you that the file is not found or that access is denied.
  • Verify the configuration is being read. Run apachectl -S to see the active configuration and which virtual hosts are loaded. Make sure the directive is in the right place.
  • Test with a simple file. Create a temporary file named exactly as you set in DirectoryIndex (e.g., test.php with content <?php echo 'test'; ?>) to see if Apache serves it. If it works, the issue is with your actual file.
  • Check for .htaccess overrides. If a .htaccess file in the directory sets a different DirectoryIndex, it will override the server configuration. Look for such files and adjust them.
  • Ensure the file is in the document root. The DirectoryIndex only applies to directories within the document root. If your file is outside, it won’t be served.

Frequently Asked Questions

Can I use a file without the word “index” in its name?

Yes, you can use any filename, such as home.php or start.html. Just set DirectoryIndex home.php and ensure the file exists in the directory.

What if I want to use a file with a different extension, like .shtml?

You can include any extension. For example, DirectoryIndex index.shtml index.html. Just make sure the module that handles that file type (like mod_include for SSI) is enabled.

Does this affect subdirectories?

Yes, the DirectoryIndex directive applies to all directories under the scope where it’s defined (e.g., globally or per virtual host). You can override it in a specific directory using .htaccess or a <Directory> block.

Why is my site showing a 403 Forbidden error after the change?

This usually means Apache can’t find the file you specified as the index. Check that the file exists and is readable by the web server. Also, ensure the directory itself is accessible (permissions).

Can I set multiple index files with different priorities?

Yes, the order in the DirectoryIndex list matters. Apache tries each file in order until it finds one that exists. For example, DirectoryIndex index.php index.html will serve index.php if present, otherwise index.html.

Do I need to restart Apache after editing .htaccess?

No, .htaccess changes take effect immediately without restarting Apache, as long as AllowOverride is set to allow them. However, changes to the main configuration files require a restart or reload.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.