Running PHP code in a file with an .html extension

Overview

Normally, when PHP and HTML are combined in the same file, the file uses a .php extension and displays data in the browser. For example, this file is named hello.php.

<html
<body>

<?php
  echo "hello";
?>

</body>
</html>

However, if you need to place PHP code in a file with an .html extension, you must add a Handler to your .htaccess file. For example, the same code above could be placed in a file named hello.html.

Code to add to your .htaccess file

View the following articles for instructions on how to edit files using an FTP or SSH client.

The file you need to add code to is named .htaccess. If this doesn't exist in your website's home directory, use the articles above to create it. Add the following line to the .htaccess file:

AddHandler php5-cgi .html

This line of code allows files with .html extensions to run PHP code in your browser.

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?