How to Parse .html or .htm Files as PHP

Sometimes, you may want your .html or .htm files to be processed as PHP so you can include dynamic code without changing the file extension. This can be done using .htaccess and PHP handlers.

1. Using CloudLinux lsapi / LiteSpeed API

If your hosting uses CloudLinux LSAPI or LiteSpeed API, add the following line to your .htaccess file in the public_html root:

AddHandler application/x-httpd-lsphp .htm .html .shtml

This tells the server to treat .htm, .html, and .shtml files as PHP.

2. Using Other PHP Handlers

For other PHP handlers, you can use one of the following methods:

Method 1: Simple Approach

Create a .htaccess file in your public_html directory with these lines:

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

Tip: Files processed as PHP may require 755 permissions for proper execution.

Method 2: Extended PHP Handler

If the above doesn’t work, try this more comprehensive handler line:

AddType application/x-httpd-php5 .php5 .php4 .php .php3 .php2 .phtml .html .htm

or

AddType application/x-httpd-php .php5 .php4 .php .php3 .php2 .phtml .html .htm

This ensures all common PHP file types, plus .html and .htm, are executed as PHP.

Notes