Parsing PHP in .HTML Files

Is parsing PHP in web pages that end in .html or .htm possible? Absolutely!

You’re suddenly hosted on an Apache web server and perhaps you’ve just moved to a host that supports PHP and you want to try your hand at inserting clever or useful PHP code in them. Then you start thinking, Oh man, now I have to rename all my web pages to newpage.php! Think again…

You may leave your web pages with the .htm or .html extensions intact and just start inserting your PHP codes and have it parsed just as if they were placed inside a .php  file!

Edit the .htaccess file

How? Well, here’s what you should do:

Go to your Document root or WWW root directory or folder; it commonly looks like this:

Look for the file named .htaccess. If it’s not there, create a blank page using a regular text editor like Notepad and save the file as .htaccess – the file name includes that little dot in the front.

Now edit this file by adding the following lines:

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

Save and close the .htaccess file. Upload it to your web server (to your Document/WWW root) and that’s it!

Sample PHP code in a .HTML webpage

Now create a test file and name it test.html

Copy the following HTML (containing PHP code) into it:


			<?php echo "I LOVE PHP!"; ?>

Upload it to your web server and view it with your favourite browser. You will see that it works just fine.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>