Running .html Files as .php Without Renaming
You need to serve existing .html files through PHP logic without touching the filesystem or rewriting hundreds of links in time for a presentation. While you might be tempted to rely on a simple rewrite rule, Apache's mod_rewrite cannot fundamentally change the MIME type of a file based solely on its extension if the server is configured to treat .html as text regardless of what happens behind the scenes.
To make an existing .html file execute PHP code, you must convince the server to process the content through the PHP interpreter before sending it to the browser. This requires modifying the .htaccess file to intercept requests for .html files and pass them to index.php or a custom handler, effectively treating the static extension as a dynamic request.
The Rewrite Rule Approach
The most direct method is to use a rewrite rule that maps any request ending in .html to an internal script, typically index.php. You can achieve this by placing the following block in your .htaccess file (assuming Apache modules are enabled):
RewriteEngine On
# Rewrite all requests for .html files to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.html$ /index.php [L,QSA]
This configuration relies on two critical conditions: !-f and !-d. The first ensures the requested .html file is not an actual physical file on the disk. If you have a real about.html file containing static text, this rule will still trigger if it's not explicitly excluded. To preserve your actual static HTML files while forcing others to run as PHP, you must refine the logic.
A more precise implementation targets only those .html files that are not static resources or do not exist physically:
RewriteEngine On
# If the .html file does not exist on disk, pass it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /index.php?url=$1 [L,QSA]
In this setup, index.php receives a URL parameter indicating the original filename (e.g., url=about). Inside index.php, you can check if the requested file exists in a specific directory or render a view based on that name. This allows your static HTML files to act as dynamic pages without renaming them in the browser address bar, although the URL structure changes from /about.html to /index.php?url=about.
The MimeType Limitation
If your goal is simply to change the response header so the browser treats .html content as text/html (which it usually does by default) while executing PHP tags within a file named test.html, you might consider changing the AddType directive. However, adding AddType application/x-httpd-php .html will cause Apache to attempt parsing every .html file as PHP. This is dangerous because standard HTML tags like <span> or CSS classes are not valid PHP syntax and will result in a fatal parse error for any non-PHP static page you haven't explicitly converted.
Therefore, the rewrite method described above is safer for mixed environments where some .html files are purely static content that you do not want to execute as PHP immediately. The rewrite rule effectively bypasses the file extension entirely, handing off control to your application logic defined in index.php.
Performance and Caching Considerations
From a performance standpoint, rewriting .html requests to index.php introduces an additional processing step for every request that matches the pattern. Apache must evaluate the Rewrite conditions, then invoke the PHP interpreter. If you have high-traffic static pages, this overhead is negligible compared to database queries or logic execution, but it is something to consider if your site relies heavily on serving pure HTML without backend involvement.
Furthermore, browser caching behavior changes. Since the URL becomes /index.php with query parameters, existing bookmarks or links pointing to page.html will break unless you update them in your documentation or codebase. If this is an emergency for a presentation where you cannot change links, ensure your development environment reflects the new routing logic immediately.
When to Use LaravelCompany Solutions
If this project eventually moves from a quick-and-dirty Apache configuration to a modern PHP stack, consider migrating to a framework that handles routing natively. Tools like those available at LaravelCompany often provide robust artisan commands and scaffolding to manage file associations and routing logic without relying on fragile .htaccess tricks. However, for an immediate deadline tomorrow, the mod_rewrite solution remains the most viable path to get existing files functioning as dynamic scripts without filesystem changes.
