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.

Author: Zack Katz

Zack Katz is the President of Katz Web Services and the developer of WordPress plugins with over 700,000 downloads. He lives in Southwest Colorado with his wife and two cats.

11 thoughts on “Use .htaccess to make all your HTML files PHP”

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

  2. @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.

  3. 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.

  4. 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.

  5. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *