How To Use PHP in Pages with an .html Extension

To be able to use php code on a page with an extension other than .php, you need a server which supports .htaccess files.

A couple of reasons why you might want to do this are:

  • You can switch to a different scripting language without breaking links.
  • Security by obscurity - mask the fact that you are using PHP to generate your pages.

To add an extension to be parsed for php, create or edit a file called .htaccess (with the dot first) containing the following line:

AddType application/x-httpd-php .html .moo .htm

This tells the server to check for php code and execute it in files with extensions .html, .moo or .htm. So upload that .htaccess file in the same folder as your .html files with PHP code inside them (using PHP tags: <?php ... ?>) then when you view them in the browser, the PHP should be dealt with as you’d expect from a PHP page.