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:
/home/akiko/public_html

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:

<html>
	<head>

	</head>
	<body>
		<h1>
			<?php echo "I LOVE PHP!"; ?>
		</h1>
	</body>
</html>

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

One thought on “Parsing PHP in .HTML Files

  1. Ursula

    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?

    Reply

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>