Parsing HTML files as PHP

Gary Oosterhuis | October 21, 2012


There are a many reasons to parse HTML files for PHP. It comes in handy when adding dynamic functionality to static website written using .html file extensions. Renaming HTML files to PHP files can often cause an undesired loss of page rank.

If the site you’re working on runs under apache you can easily make changes to the .htaccess file to allow HTML files to be parsed for PHP.

The Code

On web hosts that run two versions of PHP:

Some web hosts which run, or have run, two versions of PHP such as PHP4 and PHP5, usually have a PHP5 handler. The sample code below will parse all .html and .htm files as PHP. (This code has been tested on HostGator and InMotion hosting.)

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

On most other web hosts:

For hosts that only run a single version of PHP, the following code should work. (This code has been tested on Superb and LunaPage Web hosting.)

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

or, if the AddType directive does not work, you can use the AddHandler directive as follows:

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

or

AddHandler x-httpd-php .html .htm

 

Some web hosts, such as GoDaddy require both directives. So your code would look like this:

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

 

As a last resort, you can also try this multi-line approach which uses the SetHandler directive:

<FilesMatch "\.(htm|html|php)$">
SetHandler application/x-httpd-php
</FilesMatch>

or

<FilesMatch "\.(htm|html|php)$">
SetHandler application/x-httpd-php5
</FilesMatch>

Link Web Development is a Barrie Website Design and Development company committed to providing quality websites to business owners and other Graphic Design Firms and SEO Experts.

Add a Comment

Your email address will not be published. Required fields are marked *