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. Digicom 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. Digicom 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.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

WordPress pointing home URL to subdirectory

Pointing your home site's URL to a subdirectory In some cases, you may have a WordPress site...

How to log into cPanel

cPanel is a web hosting account management tool available on all Digicom Linux hosting plans,...

Change cPanel Password or recover lost password

Requesting a New cPanel Password If you have lost or forgotten your cPanel password, you can...

Location of Script Configuration Files

Location of Script Configuration Files These paths are relative to the root of the script, not...

Connect to MySQL Database

How to Connect to the MySQL Database In order for your MySQL database to work as intended, it...

Powered by WHMCompleteSolution