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.

However, if you need to place PHP code in a file with an .html extension, you must add a Handler to your .htaccess file.

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, depending on which version of Linux your site is running on.

If your site is on a server running Ubuntu 18

You can add either of the following.

AddHandler php-cgi .html
AddHandler php5-cgi .html

If your site is on a server running Ubuntu 14

Only the following will work.

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?