On a recent project we needed to provide a client our contribution to the site code base as static .html files. To make the development process easier though a number of the developers wanted to include some of the global page assets such as the header and footer using PHP includes in their development environments. This of course normally does not work as the server never passes the page content to the PHP engine to process as it doesn’t have a .php or .phtml extension.
Luckily its pretty easy to change the way Apache handles the .html files within a project with a simple one line .htaccess rule. First thing to check that the Apache virtual host definition for your site allows overrides from a .htaccess file. For example in the virtual host file for a site that looks like:
[codesyntax lang=”apache”]
<Directory /home/sites/testsite.com/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory>
[/codesyntax]
The AllowOverride directive would have to change to:
[codesyntax lang=”apache”]
<Directory /home/sites/testsite.com/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
[/codesyntax]
If you do not want to allow set AllowOveride to “All”, it must be set to “FileInfo” at a minimum for the AddType directive to work in the .htaccess file. Be sure to restart the web server after making any changes to the Apache configuration files to make the changes active.
Now you must create a .htaccess file in the root of your project if one doesnt already exist. If your are running PHP as an Apache module (around 9/10 server installs) you need to add the line below to your .htaccess file:
AddType application/x-httpd-php .html .htm
If the server is running PHP as a CGI service you will need to use a line containing the AddHandler directive instead:
AddHandler application/x-httpd-php .html .htm
Now load .html file within the project that contains PHP code in a browser and the code should execute as normally without the file possessing .php or .phtml extension.