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

September 14, 2010 at 9:54 am
(5) Mr. Concept says:

Great tutorial. Got a question that I can’t seem to get solved about this. I was going to add an rss feed via php from a third party website to my website. It works every way except through php. Using PHP nothing shows up. I’ve asked my host, which they told me to add the

AddHandler application/x-httpd-php5 .html .htm .php

to my .htaccess file. I did that and still nothing. I’ve asked different third party rss people and they say ask my host. My host says they are not programmers so sucks for you.

Any ideas what could be causeing this? Its a very simple line of code:

thanks
-Mr.Concept

December 13, 2010 at 10:52 am
(6) Don Grrrenfield says:

What section would your html page would you put this code?

February 7, 2011 at 5:52 am
(7) roritm says:

how to run php in html file?

—————
1. – determine which php version running on your server

2. – determine configuration of Apache PHP Handler, is it dso or cgi or SuPHP
dso – mean that php run as Apache module.
cgi or SuPHP – php run as a CGI module.

3. – add to .htaccess

for dso(php as Apache module)

AddType application/x-httpd-php .htm .html

for SuPHP, if your server run php4
AddType application/x-httpd-php4 .htm .html
or
AddType application/x-httpd-php5 .htm .html
if your server run php5

for CGI , add to .htaccess line
AddHandler application/x-httpd-php .html .htm
or
AddHandler application/x-httpd-php5 .html .htm

————————————–

AddType application/x-httpd-php(x) – for php as Apache module, Dso. x – php version
AddHandler application/x-httpd-php(x) – for php as CGI. x – php version

June 17, 2011 at 7:10 am
(8) vijedra says:

Thanks , it really works and very helpful..

September 11, 2012 at 3:41 pm
(9) John says:

doesn’t work on my site.

August 26, 2013 at 3:13 am
(10) sugan says:

please say clearly

April 16, 2014 at 12:50 pm
(11) Myles says:

This is a terrible idea, an anti-pattern that only serves to confuse the next developer unlucky enough to have been told to maintain *your* code.

If you want friendlier URLs you should be googling “mod_rewrite” and if you’re worried about people seeing what server-side language is powering your site (why on Earth would you care? ~70% of the web is powered by php…) then this doesn’t hide anything – you’d need to remove the x-powered-by header from the HTTP responses by setting “expose_php” off in your php.ini

There is absolutely no solid reason for overriding the default / standard types that can’t be countered with a better alternative!

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>
  1. About.com
  2. Computing
  3. PHP / MySQL

©2014 About.com. All rights reserved.