How do I add PHP code/file to HTML(.html) files?

345,304

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.

Share:
345,304

Related videos on Youtube

Hoon
Author by

Hoon

Updated on July 08, 2022

Comments

  • Hoon
    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 in php.ini?

    • Explosion Pills
      Explosion Pills almost 12 years
      You 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
      undone almost 12 years
    • undone
      undone almost 12 years
      @ExplosionPills I've done it using IIS v7! and it's possible!
    • Explosion Pills
      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
      Hoon almost 12 years
      In 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
      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
      Edward almost 11 years
      I 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
      Dani about 7 years
      @ExplosionPills I cannot change my homepage's extension from .html, unlike every other page I create.
    • Alien Technology
      Alien Technology almost 7 years
      What a horrible idea. Use redirects to save your SEO. Use .html for HTML and .php for PHP.
  • Explosion Pills
    Explosion Pills almost 12 years
    While 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
    nickb almost 12 years
    I've done this on numerous occasions to hide the fact that I'm using PHP (in addition to other things)
  • Hoon
    Hoon almost 12 years
    What 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
    David Morales almost 12 years
    The format should be .php, as .html files are intended to contain only HTML code.
  • Nathan Long
    Nathan Long almost 12 years
    @nickb - security through obscurity?
  • Eric Finn
    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
    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 visit example.com/foo. You could use that URL to serve PHP content regardless of the file names on your server. If users already have foo.html bookmarked, you could still serve foo.php without renaming the file.
  • Eric Finn
    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
    David Morales almost 12 years
    Remember to not write your entire answer in bold, please.
  • Kaspar L. Palgi
    Kaspar L. Palgi almost 8 years
    now when I open that .html file then it pops up and wants me to download it.
  • Randy
    Randy over 5 years
    WARNING- 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
    Floggedhorse about 5 years
    You could also try .phtml
  • carloswm85
    carloswm85 over 4 years
    Can we tell that doing this is bad practice?
  • PatrickT
    PatrickT over 4 years
    would you care to comment on the difference between AddHandler and AddType? Thanks.
  • Chris Trahey
    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
    quantme about 4 years
    Thank you for considering those who don't even know how to create a .htaccess file.
  • quantme
    quantme about 4 years
    Very practical.
  • fasterthanlight
    fasterthanlight almost 4 years
    So can I put HTML in .php files?
  • The concise
    The concise over 3 years
    @fasterthanlight Sure. Yes
  • Shaun Bebbers
    Shaun Bebbers over 2 years
    Serious 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
    George Chalhoub over 2 years
    Awesome answer!