change extension of web pages .php .html

If you need to add php code into .html (or .htm) file and you don’t want to change the file extension to .php because you already have incoming links on the .html file – although you can solve that with a simple redirect – here is the solution:

Add the following lines of code to your .htaccess file :

AddType application/x-httpd-php .html

If you just need one .html file to behave like that, then enclose that code with “files” tag like this :

AddType application/x-httpd-php .html

Pretty simple, huh? 🙂

Here is what’s Happening

when a web page is requested, the server checks the file extension to know how to handle the page. If it sees a .php extension (or .shtml, .asp etc.), it knows that it needs to execute the appropriate code before passing it along to the browser. If it sees a .htm or .html file, it sends it right to the browser because it doesn’t have anything to process on the server.
With the above piece of code, we simply tell to server to execute a .html file as if it’s a .php one.

Be aware!

* If you have an existing .htaccess file, make sure not to overwrite anything there or other settings may stop working!
* Anything in your .html files that starts with will now be executed as PHP, so if it’s in your file for some other reason (an XML tag for example) you will need to echo these lines to prevent errors.