Server Not Parsing .html as PHP: Diagnosing Apache Include Handlers
When you configure an .htaccess file to treat static HTML files as dynamic containers, it is easy to assume that simply adding the right directives will trigger execution. The code snippet you provided demonstrates a common approach using Apache's mod_include, specifically targeting server-parsed handlers to process includes within .html and .htm files. While this setup appears syntactically correct, the failure of included PHP code to execute usually points to one of two issues: the server module isn't loaded, or the execution order is incorrect.
The directive AddHandler server-parsed .htm .html tells Apache to pass these files through an output filter that processes include tags like <!--#include virtual="header.php"-->. However, this mechanism only works if mod_include is actually enabled on your specific Apache instance. If the module is disabled or compiled out of the binary, adding the handler does nothing; the file is served as plain text. To verify this, check your active modules using apachectl -M (on Linux) or httpd -M (on Windows). You should see include_module listed in the output. If it is missing, you must enable the module via a2enmod include on Debian/Ubuntu systems or add LoadModule include_module modules/mod_include.so to your httpd.conf.
Even if the module is present, there is a subtle architectural trap involving PHP execution contexts. The server-parsed handler operates as an output filter, meaning it sees the file content only after Apache has already read it and prepared it for transmission. This creates a specific constraint: any PHP code included via this method must be self-contained or processed by the same interpreter that is handling the main request.
Consider a scenario where you include header.php. If your virtual host configuration sets <FilesMatch "\.php$"> SetHandler application/x-httpd-php </FilesMatch>, the standard PHP handler will execute the included file correctly. However, if you rely on the server-parsed logic to mix static content with dynamic includes, you must ensure that the included files do not attempt to set headers or redirect before the main script finishes buffering. More importantly, this approach does not handle < ?php ?> tags inside the .html file itself unless those tags are wrapped in include directives or processed by a different handler like server-parsed-php. The standard server-parsed directive handles <!--#include--> syntax, but it does not automatically invoke the PHP interpreter for code found directly within the HTML stream unless specific conditions are met.
If your goal is to mix raw HTML with PHP logic without relying on include directives, you need to change the file extension handling entirely. The current configuration adds .html files as text/html, but if you want the server to execute PHP code inside those files directly, you should consider changing the handler for that specific directory or file pattern. For instance:
<FilesMatch "\.(html|htm)$">
SetHandler application/x-httpd-php
</FilesMatch>
This directive forces Apache to send the .html files to the PHP interpreter for processing before any output filtering occurs. This is often more reliable than server-parsed because it treats the file as a full script rather than a template with static includes. However, this approach can break existing pure HTML sites if not scoped correctly, so you might want to scope this change to specific virtual hosts or directories where you expect mixed content.
Another critical aspect is the interaction with compression filters. Your configuration includes AddOutputFilterByType DEFLATE text/html. If you switch to treating .html files as PHP scripts, ensure that your compression settings do not interfere with the execution. While modern servers handle this well, some older configurations might attempt to compress binary or executable streams incorrectly if MIME types are misconfigured. Verify that text/html remains the primary content type after PHP processing so that browsers render the page correctly rather than treating it as raw text or code.
Finally, remember that Apache configuration is hierarchical. If you have a global httpd.conf setting that overrides your .htaccess file, the local directives will be ignored. Always check for conflicting SetHandler instructions in parent directories or the main configuration file. If you are using a framework like Laravel, note that their default server configurations often rely on mod_php or fpm handlers rather than mod_include, making the server-parsed approach unnecessary and potentially problematic. For complex projects requiring mixed static/dynamic content, consider switching to Nginx with PHP-FPM, which handles include directives and MIME type mapping more explicitly through separate configuration blocks.