Loading...

Knowledge Base
Save up to  70% off.  Start your website today!

How to Execute PHP in .html Files

It is highly recommended that you never allow HTML pages to handle PHP or SHTML automatically 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 a .htaccess code that is not supported by our live support. If you experience issues implementing this solution on our Shared servers or a VPS or Dedicated server, you will need to contact a web developer for assistance.

 


Why run PHP in HTML files?

It is becoming increasingly essential to use server-side processing to verify reCaptcha attempts using the current API.

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

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

 


How do I use PHP in HTML files?

Linux Servers

While you can still manually update your website's PHP version (7.4 is the minimum), we strongly recommend updating it using the MultiPHP Manager. Please note that the minimum PHP version on our Shared hosting server's cPanel is currently 8.1. For more information, please visit the following article. 

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. Log in to your cPanel.
  2. Click the File Manager icon.

    HostGator cPanel File Manager

  3. Navigate to the site's document root you wish to change ("public_html" is the document root for your primary domain).
  4. Select the .htaccess and click Code Editor at the top (If the .htaccess file does not exist, you will need to create one). Read How to Edit Your .htaccess File for more details. 
  5. Depending on the PHP version utilized by your website, you will need to add only one of the following AddHandler scripts near the top of the .htaccess file. To know what version of PHP your site uses, please check out the article What Version of PHP Are You Using?
    • AddHandler application/x-httpd-ea-php54 .html .htm
    • AddHandler application/x-httpd-ea-php55 .html .htm
    • AddHandler application/x-httpd-ea-php56 .html .htm
    • AddHandler application/x-httpd-ea-php70 .html .htm
    • AddHandler application/x-httpd-ea-php71 .html .htm
    • AddHandler application/x-httpd-ea-php72 .html .htm
    • AddHandler application/x-httpd-ea-php73 .html .htm
    • AddHandler application/x-httpd-ea-php74 .html .htm
    • AddHandler application/x-httpd-ea-php80 .html .htm
       
  6. Click the Save button to commit your changes.

You can now 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 servers on which this method will work:

  1. Log in to Plesk.
  2. Click Websites and Domains.

    HostGator Plesk Website and Domains

  3. Click File Manager for the site on which you wish to enable this change.

    HostGator Plesk File Manager

  4. 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>
  5. If a web.config already exists, ensure that the handler code is placed in the correct Handler section.
  6. Adjust the code to use the supported PHP version, with 8.3 as the maximum PHP version available.

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

Need additional assistance? Hostgator's support is here to help. Please contact us via phone or chat so we can assist!
Did you find this article helpful?

 
* Your feedback is too short

Loading...