Use .htaccess to make all your HTML files PHP

Note: the following will not work on all server configurations, which may be why you’re using SSI in the first place. Call your server administrator/tech support and see if the following techniques are supported.

Escape static HTML

If you want the power and flexibility of PHP but don’t want to (or can’t) shift away from HTML, you can actually tell your server to read all HTML files as PHP by making one simple change to a file called .htaccess.

Here’s how:

  1. Create a file called .htaccess if you don’t have one already, and save it to your root folder
  2. Add one of the following pieces of code into your .htaccess file. Depending on your server configuration, one or the other should work…you’ll just have to try them out to see which one works for you:
    • AddType application/x-httpd-php .php .html or
    • AddHandler x-mapp-php4 .html .htm
      AddHandler x-mapp-php5 .html .htm
      (thanks to Florent V. below) or
    • <FilesMatch "\.s?html?$">
      SetHandler application/x-httpd-php
      </FilesMatch>
  3. Save the file and upload it to your server.

Now, you’re able to use all of PHP’s goodness by adding one line of code!

For GoDaddy.com

GoDaddy-hosted websites should use AddHandler x-httpd-php5 .php .shtml .html .htm instead.

This entry was posted in Code and tagged , , , , , . Bookmark the permalink.
  • http://tentativewriting.com Florent V.

    Hello,

    I doubt this will work as intended. It sure doesn’t work for me. The AddType directive only adds the given value (“application/x-httpd-php”) to the HTTP headers sent to the browser, so that you get “Content-Type: application/x-httpd-php” in the headers.

    Browsers will either ignore it or prompt for a download (because they don’t know how to handle that MIME type).

    I think you need the AddHandler directive instead. Like this:

    AddHandler x-mapp-php4 .html

    Or, for PHP5:

    AddHandler x-mapp-php5 .html

    • http://www.atelierharoldguerin.com Bobby

      Days and hours wasted, I glad i found your post and this comment.  it works now
      Thank you

      • http://www.katzwebservices.com Zack Katz

        Glad I could help!

  • http://www.katzwebdesign.net Zack Katz

    @Florent V — thanks for the comment. I noticed that this code didn’t work very well in all situations, that indeed it was flawed, but I forgot to update this post with what I found: your solution.

    I believe my original solution may only apply to 1&1 Internet hosted websites?

    Thanks for your input.

  • Web Design

    This doesn’t work for cPanel Apache servers.

    You need to use this:

    #
    # Run PHP scripts in HTML files.
    #

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

    Hope that helps soneone else.

  • http://www.funbiznow.com/ work at home

    I have been trying to get work. I will @Florent V’s solution. I hoe it work!

    Thanks

  • http://www.funbiznow.com/ work at home

    This is what I used to get my working with some help from my host…

    RewriteEngine on
    RewriteCond %HTTP_HOST ^funbiznow.com
    RewriteRule ^(.*)$ http://www.funbiznow.com/$1

    and this for my 404 error…

    ErrorDocument 404 /pagenotfound.html

    and to use for my SSI inclues…

    AddType text/html .html
    AddHandler server-parsed .html
    AddType text/html .shtml
    AddHandler server-parsed .shtml

    Hope this help.

  • http://www.suggestrestaurants.com Swapnil

    how 2 solve this problem for parallel plex servers

  • londres

    Thanks for the simple article and post!

    But for .htaccess on the GoDaddy-hosted websites don’t forget to include “.html” file extensions in your handler! Might want to edit original post…

    Should be

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

    instead of just

    AddHandler x-httpd-php5 .php .htm

    thanks again.

  • Elron

    im useing goDaddy,
    thank you so much, really helped me!

  • http://www.seodenver.com Zack Katz

    Hi londres, you’re completely right…I forgot to add a few file extensions. I’ve updated it now to include .html and .shtml. Thanks!