How to Parse HTML as PHP

PHP/MariaDB/MySQL | Updated March 2026

By default, Apache only processes files with a .php extension through the PHP interpreter. If you want .html or .htm files to be parsed as PHP (so they can contain PHP code), you need to add a handler directive to your .htaccess file. This is a common need when migrating from an older site that uses .html extensions but now needs PHP functionality.

01. The .htaccess Method

Add this line to your .htaccess file:

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

This tells Apache to send .html and .htm files through the PHP handler. You can then use PHP code inside your HTML files:

<!-- This now works in .html files -->
<p>Today's date is: <?php echo date('F j, Y'); ?></p>
Note

The .htaccess change applies to the directory it's in and all subdirectories. If you only want it for a specific subdirectory, place the .htaccess file there instead of in public_html.

02. Which Handler to Use

The correct handler depends on your server's PHP configuration. Ultra Web Hosting uses CloudLinux LSAPI, so the correct handler is:

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

If that doesn't work (you'll see the PHP code displayed as text instead of executing), try these alternatives:

# Alternative 1: Generic PHP handler
AddType application/x-httpd-php .html .htm

# Alternative 2: If using ea-php (EasyApache)
AddHandler application/x-httpd-ea-php82 .html .htm
Tip

If you're not sure which handler your account uses, create a test file called test.html with <?php phpinfo(); ?> as the content. Try each handler in .htaccess until the phpinfo page displays instead of raw PHP code. Delete the test file when done.

03. Parsing Specific Files Only

If you only need PHP parsing in one or two specific .html files (not all of them), you can target them individually:

<Files "contact.html">
  AddHandler application/x-httpd-lsphp .html
</Files>

Or use FilesMatch for a pattern:

<FilesMatch "^(contact|about|pricing)\.html$">
  AddHandler application/x-httpd-lsphp .html
</FilesMatch>

For more .htaccess techniques, see our Complete .htaccess Guide.

04. Performance and SEO Considerations

Performance

When you add the PHP handler for .html files, every .html file is processed by PHP even if it contains no PHP code. This adds a small amount of overhead per request. For a handful of files, it's negligible. For a site with thousands of static .html files, it adds up. Only apply the handler to the directory or files that actually need it.

SEO

Changing how files are processed on the server has zero impact on SEO. Google doesn't care whether a page is served by PHP or as static HTML. The URL stays the same and the output stays the same. The change is entirely server-side and invisible to visitors and search engines.

05. Alternative: Rename to .php

If you're building a new site or can easily update your links, the cleaner approach is to simply rename your files from .html to .php. This is the standard convention and doesn't require any handler configuration.

If you rename files, set up 301 redirects for the old URLs so bookmarks and search engine listings still work:

Redirect 301 /about.html /about.php
Redirect 301 /contact.html /contact.php

Or use a pattern redirect for many files:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /$1.php [R=301,L]

Need Help With PHP Configuration?

If the handler directive isn't working or you're seeing raw PHP code in the browser, open a support ticket with the directive you tried and the URL that's not working.

Open a Support Ticket

Quick Recap: Parse HTML as PHP

If you only do 5 things from this guide, do these:

  1. Add the handler to .htaccess - AddHandler application/x-httpd-lsphp .html .htm
  2. Test with a simple PHP file - <?php echo 'works'; ?> in a .html file
  3. Use <Files> for specific files only - avoids processing all .html files through PHP
  4. Consider renaming to .php instead - cleaner, no handler needed, with 301 redirects for old URLs
  5. Delete test files - never leave phpinfo or test scripts on a live site

Last updated March 2026 · Browse all PHP articles

  • 527 Users Found This Useful

Was this answer helpful?

Related Articles

Increase PHP Memory

Fatal error: Allowed memory size of xxxxxxxxx bytes exhausted (tried to allocate xxxxxxxx bytes)....

PHP open_basedir Restriction in Effect

PHP/MariaDB/MySQL | Updated March 2026 The open_basedir restriction in effect warning is a...

How to Add a User to a MariaDB/MySQL Database

PHP/MariaDB/MySQL | Updated March 2026 Every application that uses a database (WordPress,...

How do I change my PHP settings?

How do I change my PHP / PHP.INI settings?To change your PHP settings, just login to your hosting...

Running a PHP file with a cron job

To run a PHP file from cron, use the "cron jobs" section of your control panel. Use the following...



Save 30% on web hosting - Use coupon code Hosting30