How do I add PHP code/file to HTML(.html) files?
Solution 1
You can't run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. To do this you need to create a .htaccess file in your root web directory and add this line to it:
AddType application/x-httpd-php .htm .html
This will tell Apache to process files with a .htm or .html file extension as PHP files.
Solution 2
I think writing PHP into an .html file is confusing and anti-natural. Why would you do that??
Anyway, if what you want is to execute PHP files and show them as .html in the address bar, an easiest solution would be using .php as normal, and write a rule in your .htaccess like this:
RewriteRule ^([^.]+)\.html$ $1.php [L]
Solution 3
In order to use php in .html files, you must associate them with your PHP processor in your HTTP server's config file. In Apache, that looks like this:
AddHandler application/x-httpd-php .html
Solution 4
You can modify .htaccess like others said, but the fastest solution is to rename the file extension to .php
Solution 5
Add this line
AddHandler application/x-httpd-php .html
to httpd.conf
file for what you want to do.
But remember, if you do this then your web server will be very slow, because it will be parsing even static code which will not contain php code.
So the better way will be to make the file extension .phtml
instead of just .html
.
Related videos on Youtube
Hoon
Updated on July 08, 2022Comments
-
Hoon almost 2 years
I can't use PHP in my HTML pages. For example,
index.html
. I've tried using both:<? contents ?>
and
<?php contents ?>
Neither of these work. My server offers PHP, and when I use a the
.php
extension, it works properly. Is this a problem or do I have to change the preferences inphp.ini
?-
Explosion Pills almost 12 yearsYou would have to change your server (I assume apache) to serve html files as php, but why do you want the extension to be html instead of php?
-
undone almost 12 years
-
undone almost 12 years@ExplosionPills I've done it using IIS v7! and it's possible!
-
Explosion Pills almost 12 years@Death I didn't mean to imply that it can only be done with apache, I just assumed he was using apache.
-
Hoon almost 12 yearsIn fact, I changed my server recently and, my previous server, they provided php on html but the current server didn't and I was curious.
-
Hoon almost 12 years@ExplosionPills, I really appreciate your attention for my curiosity . But my point was just to solve my issue and I don't care the way. I assumed I've to change my configuration and I couldn't think adding on '.htaccess' at all. Really Thanks for helping me :)
-
Edward almost 11 yearsI imagine @Hoon might want to have the file be named .html for a variety of reasons including SEO purposes than have a .php extension.
-
Dani about 7 years@ExplosionPills I cannot change my homepage's extension from
.html
, unlike every other page I create. -
Alien Technology almost 7 yearsWhat a horrible idea. Use redirects to save your SEO. Use .html for HTML and .php for PHP.
-
-
Explosion Pills almost 12 yearsWhile this is right I think a more important question is why do this? Moreover, he should update server config rather than use
.htaccess
if that's an option. -
nickb almost 12 yearsI've done this on numerous occasions to hide the fact that I'm using PHP (in addition to other things)
-
Hoon almost 12 yearsWhat if i use PHP + HTML? which format should i use? I'm quite confused. My page has really short code based on PHP and there are many codes in HTML. So what i was going to do is to make a HTML file. Is this a bad habit?
-
David Morales almost 12 yearsThe format should be .php, as .html files are intended to contain only HTML code.
-
Nathan Long almost 12 years@nickb - security through obscurity?
-
Eric Finn almost 12 years@NathanLong w3.org/TR/cooluris Specifically, "Keep implementation-specific bits and pieces such as .php and .asp out of your URIs, you may want to change technologies later." in section 4.5.
-
Nathan Long almost 12 years@EricFinn - that's a good guideline. But having the server process
.html
files as PHP isn't necessary even, or helpful, in following it. You want users to visitexample.com/foo
. You could use that URL to serve PHP content regardless of the file names on your server. If users already havefoo.html
bookmarked, you could still servefoo.php
without renaming the file. -
Eric Finn almost 12 years@NathanLong That's fair, I suppose. But my point was that there are other reasons than 'security' for hiding the fact that PHP is being used.
-
David Morales almost 12 yearsRemember to not write your entire answer in bold, please.
-
Kaspar L. Palgi almost 8 yearsnow when I open that .html file then it pops up and wants me to download it.
-
Randy over 5 yearsWARNING- I confirm what many have pointed out, apparently long ago (and now in 2019 as of this comment). This change will cause Firefox browsers to offer the visitor a chance to download the page. Even if its fixed in SOME versions of Firefox, I don't think anyone should take a chance!
-
Floggedhorse about 5 yearsYou could also try .phtml
-
carloswm85 over 4 yearsCan we tell that doing this is bad practice?
-
PatrickT over 4 yearswould you care to comment on the difference between
AddHandler
andAddType
? Thanks. -
Chris Trahey over 4 years@PatrickT Simplest way to compare is that AddHandler is an "input" mapping - it maps an incoming file extension to a handler. AddType modifies the output MIME type header. See the note here: httpd.apache.org/docs/2.4/mod/mod_mime.html#addtype
-
quantme about 4 yearsThank you for considering those who don't even know how to create a .htaccess file.
-
quantme about 4 yearsVery practical.
-
fasterthanlight almost 4 yearsSo can I put HTML in
.php
files? -
The concise over 3 years@fasterthanlight Sure. Yes
-
Shaun Bebbers over 2 yearsSerious question: would this mean that you could somehow inject PHP into the live server by POSTing some PHP script to it or something?
-
George Chalhoub over 2 yearsAwesome answer!