Using .htaccess to Parse HTML Files as PHP
I receive this question quite frequently by readers who want to know how to configure Apache to treat HTML files as if they were PHP. Parsing HTML files as PHP can be quite handy if you have a current HTML site that you’re moving over to PHP and you don’t want to have to change URLs. It’s also quite simple to do. It only takes one line in your .htaccess file.
AddType application/x-httpd-php .php .htmlAdding this line to your .htaccess file will add a new MIME type your Apache configuration. This tells Apache to recognize any of the provided file extensions as PHP. You can add as many extensions as you like. Just keep separating them with spaces.
Keep in mind, that these settings will override your server defaults.





























October 3rd, 2010 at 4:52 pm
[…] du site, alors que le tag php demande soit des pages en .php, soit un paramétrage du serveur pour traiter les pages html comme du php. Pour un produit « grand public », le choix du Javascript parait au première […]
November 23rd, 2010 at 5:45 am
hi thanks for share
February 7th, 2011 at 8:40 am
Hello.
Thank you but this method is very brutal. Does is have any drawback is some .html or .xhtml pages does not contain any PHP?
Isn’t it possible to say to Apache to serve some specified HTML or XHTML pages as PHP?
February 7th, 2011 at 10:57 am
This method is not brutal at all. In fact, it’s a pretty standard practice. The parser is only going to parse files which contain PHP tags. Everything else is going to be pushed through anyway. I guess I’m not sure why you think this is a bad idea.
February 7th, 2011 at 11:15 am
Thank you for your quick answer.
I think it’s not a good idea for the reason explained there in the “note of warning”:
http://www.electrictoolbox.com/apache-parse-html-as-php/
February 7th, 2011 at 11:36 am
While it is true that the PHP parser is invoked, the parser exits immediately if there are no PHP tags. Nonetheless, this is all a moot point. The idea behind this is that you WANT the files to be parsed as PHP so that PHP content can be named with an HTML file extension. The files themselves are, in fact, PHP files. If the PHP parser were not invoked, you wouldn’t be serving any pages at all.