Skip to content

Running PHP code in .html files

By default, PHP code only runs in files ending in .php. In a .html or .htm file, PHP code isn’t run: visitors may see the code itself, or nothing at all.

Section titled “Option 1: Rename the files to .php (recommended)”

Renaming about.html to about.php is the simplest and fastest option, because the server doesn’t have to check every HTML page for PHP code.

To keep your old addresses working, add a redirect to the .htaccess file in the same folder:

RewriteEngine On
RewriteRule ^(.*)\.html$ /$1.php [R=301,L]

Update the links in your pages to point to the .php files too.

Option 2: Tell the server to run PHP in .html files

Section titled “Option 2: Tell the server to run PHP in .html files”
  1. In cPanel’s File Manager, open your website’s folder, for example public_html. Click Settings and tick Show Hidden Files.
  2. Edit the .htaccess file (create it if it doesn’t exist) and add this line at the top:
AddHandler application/x-httpd-lsphp .html .htm
  1. Open one of your .html pages in your browser to check that the PHP code runs.

This uses the PHP version you’ve chosen in Select PHP Version.

Was this page helpful?