Main Menu
Sub Menu
- Article Ref
- 1059-QGKN-1724
- Written By
- anand
- Date Created
- Thu, 22nd Nov 2007
- Updated By
- CIAN Technologies Inc. Staff
- Date Modified
- Wed, 21st May 2008
[HOW-TO] Parse PHP in .html or other files
Question
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
Answer
SOLUTION
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
</FilesMatch>
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.
NOTES:
- 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
UPDATE
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.
Related Articles
| Article Title | Date Created | Article Views | Article Rating |
|---|---|---|---|
| [HOW-TO] Fixing ForceType & Other Apache .htaccess Errors (PHP as a CGI) | Thu, 22nd Nov 2007 | 4340 | ![]() |
Article Comments
Comment posted Wed, 21st May 2008
It doesn't work after migrating from PRIMUS to iPRIMUS
Comment posted
THANK YOUUUUUUUU!!!!!!!!!!!!!!!!
for cryin out loud i been lookin for this to work for apache 2.2.x for a while now and this is the only code i found to work.
the UPDATE for Apache 2.2x
thanks soooo much, you guys make my job easier!




