1. Home
  2. Computing & Technology
  3. PHP / MySQL
photo of Angela Bradley
Angela's PHP / MySQL Blog

By Angela Bradley, About.com Guide to PHP / MySQL

Execute PHP from a .html File

Wednesday May 17, 2006
Usually when we run PHP, we put it in a file with a .php extension. Sometimes however, we need to execute PHP code on a .html page. The easiest way would be to simply rename the page from yourfile.html to yourfile.php. This however can cause problems if the page has been around awhile and has lots of incoming links, or is already ranked in search engines, and you don't want to change it's name.

The solution is to modify the .htaccess file to make PHP executable on .html files. By doing this you can make all your .html/.htm files run PHP, or you can set it to simply process one specific page (yourpage.html for example) as PHP.

Some hosting companies also allow you to change this setting from your control panel, or if you contact them they may be able to change this setting for you.
Comments
May 24, 2006 at 1:03 pm
(1) Yaseen says:

This is a good idea to hide the languages we use in order to run our dynamic websites!

Thanks

June 2, 2008 at 11:01 am
(2) Tony says:

I must have missed something. I added the necessary 2 lines of code to my .htaccess file, and inserted the DATE function into the page linked here. But it’s still not displaying.

January 11, 2009 at 6:15 pm
(3) Michael says:

Just do it the standard way: 301 redirect with .htaccess

Redirect 301 /page.html http://www.example.com/page.php

This will make google reindex the page (so new users will be looking at the correct page) and old users bookmarks will stay working.

I do not consider having php parse some html files to be good practise and I advise against it.

October 14, 2009 at 9:00 am
(4) CoDE says:

Great idea. But if you need to change the filetype of a page, you can simply wipe your old page and put a JavaScript redirect on it. :)

CoDE

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

©2009 About.com, a part of The New York Times Company.

All rights reserved.