How to make php code run in .html files

This is a fairly simple one. If your website is hosted on an Apache Web Server then you can just edit or add a file called .htaccess. Inside this file you will need to add a line

addType application/x-httpd-php .php .phtml .html

This tells the web server that files ending in .php .phtml and .html are to be checked for PHP code.

By using the .htaccess method you will not need to restart apache to pick up the changes as apache looks for the .htaccess file each time a page is requested.

If you have to create an .htaccess file you will need to make sure the file permissions on it are 644. Or if setting via an ftp package Owner = Read/Write and the Group and Other will be set to READ only.

You should now be able to put sections anywhere in your html pages.

So why would you want to do this? There are a number of reasons, some of which are

  • Standard Header and Footers
  • Standard Menus
  • Displaying Date & Time
  • Displaying information from a database

Leave a Reply

You must be logged in to post a comment.