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
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.