Learning Journal

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.

1 reply on “Parsing PHP in .HTML Files”

Nice tip, very well written. However for some reason doesn’t work with my site… I must’ve messed something up although I have no idea what since it’s all so simple 🙁 As I understand .htaccess needs to be in the same directory as my *.html file, right?

Leave a Reply to Ursula Cancel reply

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