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:
- Create a file called .htaccess if you don’t have one already, and save it to your root folder
- 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
orAddHandler x-mapp-php4 .html .htm
(thanks to Florent V. below) or
AddHandler x-mapp-php5 .html .htm<FilesMatch ".s?html?$">
SetHandler application/x-httpd-php
</FilesMatch>
- 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.
11 replies on “Use .htaccess to make all your HTML files PHP”
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
Days and hours wasted, I glad i found your post and this comment. it works now
Thank you
Glad I could help!
@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.
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.
I have been trying to get work. I will @Florent V’s solution. I hoe it work!
Thanks
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.
how 2 solve this problem for parallel plex servers
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.
im useing goDaddy,
thank you so much, really helped me!