How to Execute PHP in .html Files

It is highly recommended that you never allow HTML pages to automatically handle PHP or SHTML because this forces all of your HTML pages to be processed by the server first. Instead, please rename your files to .php or .shtml whenever possible.

This is an advanced tutorial involving using .htaccess code that is not supported by our live support. If you experience issues implementing this solution on our servers, or on a VPS or Dedicated server, you will need to contact a developer for assistance.

Why Run PHP in HTML Files?

It is becoming increasingly essential to use server side processing to perform tasks such as verifying reCaptcha attempts using the current API.

One of the best and easiest ways to include server side processing in your web pages is with PHP scripting. By default, most servers only attempt to execute the PHP scripts in files that use the .php extension. As a best practice, you may wish to consider creating new pages as .php files so that they are build from the beginning to be able to use PHP.

However, sometimes this is not possible for existing pages as it can have a number of adverse effects. Changing the file extension can break Javascript, and even disrupt old links that directly reference your .html or .html files.

How Do I Use PHP in HTML Files?

Linux Servers:

The method you use depends on the version of PHP you use on your server. HostGator maintains updated versions of PHP across our Linux Shared and Reseller servers on which this method will work:

  1. Login to your cPanel.
  2. Click the File Manager icon.
  3. Navigate to the document root for the site you wish to change (public_html is the document root for your primary domain).
  4. Select the file titled .htaccess and click Code Editor at the top (If the .htaccess file does not exist you will need to create one).
  5. Add the following lines near the top of the file:
    AddHandler application/x-httpd-php5 .html .htm
  6. Click the Save button to commit your changes.

You will now be able to include PHP scripts with your existing HTML that will be executed server side when your site is loaded.

Windows Servers:

The method you use depends on the version of PHP you use on your server. HostGator maintains updated versions of PHP across our Windows Shared servers on which this method will work:

  1. Login to your control panel.
  2. Click File Manager for the site you wish to enable this change on.
  3. Create a web.config file if one does not already exist with the following contents:
    <?xml version="1.0"?>
    <configuration>
    <system.webServer>
    <handlers>
    <add name="PHP53_via_FastCGI_HTML" path="*.html" verb="GET,HEAD,POST" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\Parallels\Plesk\Additional\pleskPHP53\php-cgi.exe" resourceType="File" requireAccess="Script" />
    </handlers>
    </system.webServer>
    </configuration>
  4. If a web.config already exists, ensure to place the handler code in the correct Handler section.
  5. Adjust the code to use PHP 5.2 or 5.4 by changing the instances of PHP53 to either PHP5 or PHP54 depending on the version used by your server.

You will now be able to include PHP scripts with your existing HTML that will be executed server side when your site is loaded.