Using .htaccess to Parse HTML Files as PHP

Written by Nilpo in Apache, Web Development | 6 Comments

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 .html

Adding 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.

Tags

Like the read? Share it!

6 Comments

  • […] 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 […]

  • hi thanks for share

  • 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?

  • 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.

  • 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/

  • 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.

Leave a Reply

Contact

Wanna say hello?
Drop us a line!

You'll find us here

1 Microsoft Way,
Redmond,
WA 98052, United States