Use .htaccess to make all your HTML files PHP

April 17, 2008

in Code

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.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Twitter

Related posts:

  1. How to Add www. – or Remove www. – From Your URL
  2. WordPress Comment Form 404 Error – Now Fixed!

{ 10 comments… read them below or add one }

Florent V. June 26, 2008 at 4:50 am

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

Reply

Bobby February 10, 2010 at 3:21 pm

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

Reply

Zack Katz February 10, 2010 at 7:33 pm

Glad I could help!

Reply

Zack Katz June 26, 2008 at 8:16 am

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

Reply

Web Design December 6, 2008 at 6:39 am

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.

Reply

work at home March 5, 2009 at 7:21 pm

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

Thanks

Reply

work at home March 14, 2009 at 9:25 am

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.

Reply

Swapnil May 27, 2009 at 4:37 pm

how 2 solve this problem for parallel plex servers

Reply

londres June 17, 2009 at 12:39 pm

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.

Reply

Zack Katz June 25, 2009 at 3:53 pm

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!

Reply

Leave a Comment

Previous post:

Next post: