Using .htaccess to Parse HTML Files as PHP

October 10th, 2008

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.

[donotprint] [/donotprint] Please use the trackback link when linking to this post.

Related Posts:

Add to Technorati Favorites

6 Responses to “Using .htaccess to Parse HTML Files as PHP”

  1. Interview de l’auteur de Crawltrack : Jean Denis Brun Says:

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

  2. business websites Says:

    hi thanks for share

  3. Fiable.biz Says:

    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?

  4. Nilpo Says:

    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.

  5. Fiable.biz Says:

    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/

  6. Nilpo Says:

    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

.htaccess Apache article articles by Nilpo ASP ASP Free automation clipboard copy desktop Dev Shed docx Internet Explorer message box Microsoft PHP registry remove script scripting text tutorial tweak tweets Twitter UAC updates User Account Control VBS VBScript vista volume Windows Windows 7 Windows Guru Windows Script Windows Script Host Windows Scripting Windows Vista Windows XP Word WScript wscript.exe WSH XP