Why Your index.php Triggers a 500 Error While index.html Works Fine
Having a static site that relies on index.html is straightforward, but introducing index.php without proper configuration often leads to immediate failures. In your specific case, the server successfully serves the HTML file but crashes when it attempts to process the PHP entry point. This discrepancy isn't just about file placement; it's about how the web server interprets the request and what happens behind the scenes when that PHP code tries to run.
The fact that ../contact-us.php works correctly is a critical diagnostic clue. It tells us two things: first, your PHP installation itself is functional, and second, the Apache/Nginx environment is capable of executing scripts. The issue is isolated specifically to the root directory's handling of index.php.
The Missing Link: .htaccess Configuration
When you switch from a static file to a dynamic one, the web server needs explicit instructions on how to handle that request. If your virtual host or Apache configuration does not explicitly include directives like AddHandler, Action, or DirectoryIndex, the server might be ignoring the .php extension entirely for the root folder.
You mentioned using .htaccess with php_flag display_errors 1. While this flag is useful for debugging, it only reveals that an error occurred; it doesn't solve the root cause. The error message "Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument" suggests that Apache tried to render a custom error page but failed to execute the PHP script that generated the original request in the first place.
If you switch ErrorDocument to empty and see a 404 instead, it indicates a routing conflict. The server likely doesn't know that index.php should be executed as a script rather than served as a static file. Without a directive like Options +FollowSymLinks or proper DirectoryIndex settings pointing specifically to index.php, Apache might default to looking for index.html only, or it might fail to invoke the PHP handler entirely if the MIME type isn't set correctly.
Debugging the 500 Error Properly
Before diving into complex rewrite rules, ensure that the PHP runtime can actually parse the file. A common pitfall involves syntax errors in the index.php file itself. Even a missing semicolon or an unclosed bracket will trigger a 500 error if error reporting is suppressed.
Set up your .htaccess to expose all PHP errors temporarily:
php_flag display_errors On
php_value error_reporting E_ALL
If the output shows a fatal error (like "Call to undefined function" or syntax issues), fix the code first. However, if you see no specific error message despite display_errors being on, the issue is almost certainly in the Apache configuration directives governing the root directory.
Verifying the Script Handler
Check your Apache virtual host configuration or the main config file for lines that define how .php files are handled. You need to ensure that the PHP module is enabled and that the handler is active for the specific directory:
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
If this block is missing, Apache might treat .php files as plain text or redirect them to a different handler, causing the 500 error when the PHP interpreter fails to take over. Additionally, verify that mod_rewrite is enabled if you are relying on rewrite rules to serve content, though for a simple index file, explicit handlers are safer.
The Role of ErrorDocuments
Your observation about ErrorDocument behavior is significant. When a 500 error occurs during request processing, Apache attempts to substitute the standard error page defined by ErrorDocument 500. If that error document itself relies on PHP (e.g., <FilesMatch "500\.php"> or similar logic), and the environment isn't configured correctly for that specific file, you get a recursive failure.
If you remove the custom error page entirely by setting it to empty, the server falls back to its default behavior, which in some configurations results in a 404 if the internal redirect logic fails. This reinforces that the initial request to index.php never successfully invoked the PHP interpreter.
Next Steps
To resolve this, focus on ensuring that the root directory explicitly allows PHP execution and that the handler is correctly mapped for .php files. Review your .htaccess file to ensure no conflicting rules are blocking the execution context. If you are using a shared hosting environment, check if your provider has disabled PHP execution in specific directories or if there are restrictions on open_basedir paths that might be preventing access to necessary libraries within your index.php.
Once the handler is correctly assigned and syntax errors are ruled out, switching from index.html to index.php should work seamlessly without triggering a 500 error.
