Putting PHP Code into a Static HTML page

September 20, 2010

Often you have an old site built with .htm pages that just can’t use PHP code on it because the server wont run PHP on a static HTML page. I’ve struggled with this over and over – do I use Iframes, do I use JavaScript (Can do both but not great for search engines).

Finally today I have found out that as long as you are on an Apache server and can create or modify an .htaccess file then you can put PHP code into your static HTML page.

Here’s the code that goes into your .htaccess file at the web root (same directory as your homepage):

AddType application/x-httpd-php .htm

*Replace .htm with .html if your pages are named with .html extensions.

Once you’ve done that, test it out. Stick something like this in the code of your HTML page:

< ?php echo ("PHP is ON"); ?>

If that works then PHP is ON will be displayed wherever you inserted the code on your live page.

The reason I wanted to put PHP into my static HTML pages is that many of those old pages are well indexed in search engines. If I changed my old pages to php pages I would inevitably take a dive in most search engines until the new pages were indexed. I wanted easy ways to update my pages and using little PHP menus and inserts all over the page would help me do site-wide updates very quickly. I have been persevering with Dreamweaver templates, updating the site then uploading the entire thing by ftp onto the server but it is a slow and cumbersome process. Once you can add PHP into your static HTML pages you can do site-wide updates by changing just one file, or one line in a database.

{ 5 comments… read them below or add one }

annonymous September 20, 2010 at 12:39 pm

This is really bad form. You are now making the server parse every single HTML or HTM page on the entire website. Sure, doing this achieves your goal, but it also doubled the server processing time on every single (formerly) static page, and made your entire website unscalable!

You could handle your SEO issues by using server redirects and server remappings.

The best thing to do is not include page extensions in your architecture when you begin creating your website. This can be accomplished simply by mapping to a directory instead of a file – this method of architecture is inherently more scalable and doesn’t limit you to the technologies of today

Dave September 20, 2010 at 1:56 pm

annonnymous, thanks for your input. I saw that htaccess thing on Text Link Ads website and figured it was just what I had been looking for. I’m not too hot on server side knowledge so this is invaluable info you are giving me. It is a rough hack and I’d much rather be able to do what you mentioned.
Any hints on how to map to a directory? Is it like the mod-rewrite thing that Wordpress has to make the URL’s seem static?

Michael January 11, 2011 at 1:28 am

Why dont you just tell the .htaccess file to mod rewrite file extensions so that all the files exist on the server as php files but these are displayed as being .htm files. I use this technique quite a lot to preserve the URLs for .htm pages and any other page extension that is used by legacy pages within the site as a means of preserving historical data. Hope that helps

Disposable guy May 28, 2011 at 7:56 pm

I’m going to try this on my site. thanks for your tip Dave.

PHP Freelance November 28, 2012 at 7:09 pm

You could also use mod rewrite, but yes, this is quick and easy and I wouldn’t hesitate to recommend to older sites because it’s easy and doesn’t degrade performance as much as the first person would scare you into believing if you have a decent server.

Leave a Comment

Previous post:

Next post:

microsoft search engines