Running PHP code in a file with an .html extension

 

Overview

This article explains how to use an .htaccess file to run PHP code within an HTML file.

Background

Normally, when PHP and HTML are combined in the same file, the file uses a .php extension to display the content in the browser. For example, the following 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 first add a handler to your .htaccess file so the PHP code is able to function.

Using an .htaccess file

You must use an .htaccess file to run PHP code. If this file doesn't exist in your website's home directory, refer to the following articles for instructions on how to create the file using FTP or SSH:

Add these lines to the .htaccess file.

This code also changes the version of PHP you have set in the panel to the version in the second line.

AddHandler fcgid-script .html
FcgidWrapper "/dh/cgi-system/php83.cgi" .html

When you load the .html file in a browser, it now loads like a normal PHP file.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?