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.conforapache2.conf) or a virtual host configuration file. - Command-line access to restart Apache (e.g.,
sudo systemctl restart apache2orsudo service httpd restart). - Basic familiarity with editing text files on your server.
Step-by-Step
- Locate the Apache configuration file. The main configuration file is often named
httpd.conf(on RHEL/CentOS/Fedora) orapache2.conf(on Debian/Ubuntu). Virtual host files are usually in a directory like/etc/apache2/sites-available/or/etc/httpd/conf.d/. - Open the configuration file in a text editor. Use a command like
sudo nano /etc/apache2/apache2.conforsudo vi /etc/httpd/conf/httpd.conf. You need root privileges to edit these files. - Find the
DirectoryIndexdirective. Search for the line that starts withDirectoryIndex. It is typically inside a<IfModule dir_module>block. If you don’t find it, you can add it yourself. - Modify the directive. The
DirectoryIndexdirective sets a list of file names to look for, in order. For example, to makeindex.phpthe first choice, change the line to:DirectoryIndex index.php index.html
This tells Apache to serveindex.phpif it exists, and fall back toindex.htmlif not. You can list any number of files, and you can also use a wildcard likeindex.*if you want Apache to match any file starting withindex.. - Save the file and exit the editor. In nano, press
Ctrl+Oto save andCtrl+Xto exit. In vi, pressEsc, then type:wqand press Enter. - Test the configuration (optional but recommended). Run
apachectl configtestorsudo apache2ctl configtestto check for syntax errors. If you seeSyntax OK, you’re good to go. - Restart Apache. Use the appropriate command for your system:
sudo systemctl restart apache2(Debian/Ubuntu) orsudo systemctl restart httpd(RHEL/CentOS). You can also usesudo service apache2 restartorsudo service httpd restart. - Verify the change. Visit your website’s root URL (e.g.,
http://example.com/). If you placed a file namedindex.phpin the document root, it should now be served instead ofindex.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
DirectoryIndexdirective 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.phpbutstart.phpis 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.htmlin 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 withindex.(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
.htaccessfile in a specific directory to set a differentDirectoryIndexfor that directory only, without affecting the global configuration. For example, in.htaccessyou can add:DirectoryIndex index.php. - Check file permissions. The web server user (e.g.,
www-dataorapache) 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.logor/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 -Sto 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.phpwith 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
.htaccessfile in the directory sets a differentDirectoryIndex, it will override the server configuration. Look for such files and adjust them. - Ensure the file is in the document root. The
DirectoryIndexonly 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.