PROBLEM: You want to parse PHP code in your .html or .htm file without changing the file extension to .php or .phtml. This is generally useful for SEO purposes, but can slow down non-PHP pages if in widespread use.
Documentation usually tell you to use the following code:
However, you'll soon realize this produces an internal server error, as we use phpsuexec and PHP as a CGI
After hours of trying to debug and adjusting to find the right code, it turns out it's much simpler to fix:
This will globally make all your .html, .htm files parse PHP. If you only have a few files needing this feature to parse PHP, it is much more efficient and recommended to set a FilesMatch directive to only target those files that need the change.
AddHandler application/x-httpd-php .php .htm .html
The above code would only affect a page called site.html. Of course, as Apache is quite powerful, you can customize to target certain directories, file patterns and more, with some tweaking.
- The directive
although apparently helpful for PHP installations compiled as a CGI fails to work. Only the above AddHandler method was successful.
- Need to parse PHP on extensionless files or other ForceType directives? See http://forums.polurnet.com/index.php?showtopic=620
For our servers with Apache 2.2.x, the above AddHandler function will not work. There is an alternate way that has been tested to work with it below:
#apply for a single file <FilesMatch "file.html"> AddHandler application/x-httpd-php5 .html </FilesMatch> #apply for all files with following extensions AddHandler application/x-httpd-php5 .php .shtml .htm .html
This will effectively parse your non-.php extension files with PHP. Note the "x-httpd-php5" which means it is using the Apache php5 module, as Apache 2 changes the way php is installed on our servers.