Make PHP to work in your HTML files with .htacess
Web Hosting Articles »
A simple guide to .htaccess »
Make PHP to work in your HTML files with .htacess
By default most web servers across the internet are configured to treat as PHP files only files that end with .php. In case you need to have your HTML files parsed as PHP (e.g .html) or even if you want to take it further and make your PHP files look like ASP, you can do the following:
For web servers using PHP as apache module:
AddType application/x-httpd-php .html .htm
For web servers running PHP as CGI:
AddHandler application/x-httpd-php .html .htm
In case you wish to do the ASP mimick:
For PHP as module:
AddType application/x-httpd-php .asp
OR
For PHP as CGI:
AddHandler application/x-httpd-php .asp
- How to block users from accessing your site based on their IP address
- How to prevent or allow directory listing?
- How to change the error documents – 404 Page Not Found, etc
- Using .htaccess for password protecting your folders
- Using .htaccess to block referrer spam
- Disable Hot-Linking of images and other files
- Redirect URLs using .htaccess
- Introduction to mod_rewrite and some basic examples
- Force SSL/https using .htaccess and mod_rewrite
- 301 Permanent redirects for parked domain names
- Enable CGI, SSI with .htaccess
- How to add Mime-Types using .htaccess
- Change default directory page
- Block Bad robots, spiders, crawlers and harvesters
- Make PHP to work in your HTML files with .htacess
- Change PHP variables using .htaccess
- HTTP Authentication with PHP running as CGI/SuExec
- Force www vs non-www to avoid duplicate content on Google
- Duplicate content fix index.html vs / (slash only)
Comments 36 >>
Jamie Said,
Oct 14, 2006 @ 09:48
Bit Pointless, as it's security by obscurity. Which always ends up not being worthwhile.
Vince Said,
There are many other uses of making your PHP files look like HTML pages.
Take SEO for example.
Oct 14, 2006 @ 10:53
It is not all about security :)There are many other uses of making your PHP files look like HTML pages.
Take SEO for example.
Jon Said,
I am planning to use php includes to rebuild my pages. Currently all my pages are .html
One suggestion was to change to .php but this will cause all links to break, including all important search listings.
Is placing this code in the htaccess file my solution ? i.e. I do not need to change the names of the files?
AddHandler application/x-httpd-php .html .htm
My tech skills are pretty low, so looking for a simple solution. I do not have access to the Apache thing on my server (which a friend suggested).
Cheers
Jon.
Nov 10, 2006 @ 06:21
HiI am planning to use php includes to rebuild my pages. Currently all my pages are .html
One suggestion was to change to .php but this will cause all links to break, including all important search listings.
Is placing this code in the htaccess file my solution ? i.e. I do not need to change the names of the files?
AddHandler application/x-httpd-php .html .htm
My tech skills are pretty low, so looking for a simple solution. I do not have access to the Apache thing on my server (which a friend suggested).
Cheers
Jon.
vince Said,
This should work fine for you.
However, whether you should use AddHandler or AddType depends on how your PHP is configured.
May be it would be best to consult your web hosting company about their particular setup.
Nov 10, 2006 @ 17:55
Yes, JonThis should work fine for you.
However, whether you should use AddHandler or AddType depends on how your PHP is configured.
May be it would be best to consult your web hosting company about their particular setup.
Jon Said,
AddType application/x-httpd-php .htm .html
-- results in attempted download
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
- results in attempted download
AddType x-httpd-php .php .htm .html
AddHandler application/x-httpd-php .php .htm .html
-- prevents php being read in php file (e.g. http://www.tranquillizer.co.uk/supplement-whey-protein.php)
and does nothing for .html file (e.g. http://www.tranquillizer.co.uk/phptest.html)
Do .php pages rank any differently from .html? Maybe I will go the route of renaming all pages to .php and setting up redirects. It probably will not be much more work to start with.
Do you know about mod re-writes? I came across this somewhere, but am not sure if that will help either! Still so much to learn.
Cheers,
Jon.
Nov 13, 2006 @ 05:42
I contacted 1and1, still waiting for some advice. I have now tried the following, and all have failed:AddType application/x-httpd-php .htm .html
-- results in attempted download
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
- results in attempted download
AddType x-httpd-php .php .htm .html
AddHandler application/x-httpd-php .php .htm .html
-- prevents php being read in php file (e.g. http://www.tranquillizer.co.uk/supplement-whey-protein.php)
and does nothing for .html file (e.g. http://www.tranquillizer.co.uk/phptest.html)
Do .php pages rank any differently from .html? Maybe I will go the route of renaming all pages to .php and setting up redirects. It probably will not be much more work to start with.
Do you know about mod re-writes? I came across this somewhere, but am not sure if that will help either! Still so much to learn.
Cheers,
Jon.
vince Said,
No, PHP files do not rank different, however if you already have your HTML files indexed it will be easier for you to add some interacitivity or dynamic content generation without losing your SE rankings.
Nov 16, 2006 @ 16:55
You will need to ask 1and1 about their particular setup.No, PHP files do not rank different, however if you already have your HTML files indexed it will be easier for you to add some interacitivity or dynamic content generation without losing your SE rankings.
E Said,
Meanwhile I whish there was a solution to this problem.
Mar 25, 2007 @ 22:13
Yes I have the same problem with 1and1. I contacted them but they will not help. Basically their responce was that I need to learn scripting, but they won't tell which kind. This is really awful responce especially that they are doing advertisement on OUR domains that we PAY for. I think a class action suit is called for!Meanwhile I whish there was a solution to this problem.
Dennis Said,
I had the same problem but finally found the solution:
AddType x-mapp-php4 .html .htm
Found here: http://faq.1and1.com/scripting_languages_supported/configuring_apache_server_using_htaccess/4.html
Jul 12, 2007 @ 11:14
Hey Jon & EI had the same problem but finally found the solution:
AddType x-mapp-php4 .html .htm
Found here: http://faq.1and1.com/scripting_languages_supported/configuring_apache_server_using_htaccess/4.html
Kimber Said,
Any idea why?
Thanks
Jul 20, 2007 @ 07:49
I have alway used this method with very little problem but recently have been getting error message. I get a lot of error but it comes down to (include_path='.:/usr/lib/php:/usr/local/lib/php') Any idea why?
Thanks
bailey Said,
Sorry, if this bothers people but how exactly is a hosting company going to afford people and the time to walk you through apache mod rewrites for a few bucks a year.
Now that I got that off my chest, 1and1 and most shared hosting companies run php in cgi mode for performance reasons.
Love this site.
Nov 05, 2007 @ 09:45
As for web hosting companies, if you want someone to hold your hand do not go for discount hosting like 1and1 or go daddy or any of cheap hosting companies. You either spend money for tech support to teach you or your time to learn how.Sorry, if this bothers people but how exactly is a hosting company going to afford people and the time to walk you through apache mod rewrites for a few bucks a year.
Now that I got that off my chest, 1and1 and most shared hosting companies run php in cgi mode for performance reasons.
Love this site.
johnrobin Said,
Feb 05, 2008 @ 04:44
are there any security issues with this setting? I'm trying to test some php file for this..
Anupam Said,
Do I also need to add something within each individual file. I think I read it somewhere sometimes back.
Jul 11, 2008 @ 02:05
I have tried everything mentioned here and still nothing works. I still get the download option for index.htmlDo I also need to add something within each individual file. I think I read it somewhere sometimes back.
glasses Said,
Jul 25, 2008 @ 11:09
I have test the host lunerpages, it works with the code AddType x-mapp-php4 .html .htm
Gambling News Said,
AddType application/x-httpd-php .html
How can I add one other file name so it allows the PHP code to work on two seperate URLs only.
Aug 01, 2008 @ 12:41
I use the code below to allow PHP to run on index.html.AddType application/x-httpd-php .html
How can I add one other file name so it allows the PHP code to work on two seperate URLs only.
Posicionamiento web Said,
Oct 11, 2008 @ 14:07
Great article. Very useful when you work with different hostings. I also had a problem with 1an1...with a domain. Their help service is awful.
Web Design Said,
Place this code in your .htaccess file:
AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php .htm
Dec 06, 2008 @ 11:19
The way to do it on a cpanel server with .htaccess enabled is as follows:Place this code in your .htaccess file:
AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php .htm
Price Buddy Said,
Jan 12, 2009 @ 16:15
Whilst developing the new design Price Spin shopping price comparison website we had to decide between using a .htaccess url rewrite or make the apache handler interpret our html files as php to enable our price comparison website to run php within out html files.
DJ Equipment Said,
AddHandler application/x-httpd-php .phtml
AddHandler application/x-httpd-php .phtm
Feb 05, 2009 @ 04:46
We found that when we used phtml files generated by Zend studio we had to use the following:AddHandler application/x-httpd-php .phtml
AddHandler application/x-httpd-php .phtm
Price Comparison Said,
Feb 13, 2009 @ 11:26
No idea why you would want to name your php files 'phtml'. If you were to do that you may as well set the apache handler to interpret standard 'html' files as php as per the above method.
dP Said,
Thanks,
DP
Apr 15, 2009 @ 21:24
I know this tread is old but i just wanna check does this script work in php5+ cuz i can get it to work on v4.4.9 but not when i switch to 5.Thanks,
DP
icy Said,
AddHandler application/x-httpd-php5 .html .htm
AddType x-mapp-php5 .html .htm
May 16, 2009 @ 15:35
i tried these separately in .htaccess, when i viewed the .html file, i got misconfiguration error. does anyone know why? pls help. thanks.AddHandler application/x-httpd-php5 .html .htm
AddType x-mapp-php5 .html .htm
San Diego Web Design Said,
AddHandler application/x-httpd-php5 .html
Jun 22, 2009 @ 16:01
Assuming you are usng PHP 5, use the following line in your .htacess file:AddHandler application/x-httpd-php5 .html
sanjay Said,
AddHandler application/x-httpd-php .html .htm
AddType application/x-httpd-php .html .htm
now the BROWSER(s) chrome, and IEe want to treat these .html and .htm as applications.
will not open them, but instead want to download them. Can you believe it. There has to be a way to stop this happening.
thanks for your help.
Jul 04, 2009 @ 15:04
I added two lines in .htaccessAddHandler application/x-httpd-php .html .htm
AddType application/x-httpd-php .html .htm
now the BROWSER(s) chrome, and IEe want to treat these .html and .htm as applications.
will not open them, but instead want to download them. Can you believe it. There has to be a way to stop this happening.
thanks for your help.
Tony Said,
Jul 08, 2009 @ 17:10
Can someone please give an explenation to why the file downloads? Thanks.
vince Said,
First of all you should not use both directives:
AddHandler application/x-httpd-php .html .htm
AddType application/x-httpd-php .html .htm
It should be either AddHandler or AddType depending on your webserver setup. If your server is running PHP as CGI, then you should use AddHandler, otherwise it should be AddType.
So try with only one of them.
Jul 09, 2009 @ 03:12
Not sure,First of all you should not use both directives:
AddHandler application/x-httpd-php .html .htm
AddType application/x-httpd-php .html .htm
It should be either AddHandler or AddType depending on your webserver setup. If your server is running PHP as CGI, then you should use AddHandler, otherwise it should be AddType.
So try with only one of them.
Oguzhan Basar Said,
.htaccess
AddType application/x-httpd-php4 .htm .html
AddType application/x-httpd-php5 .htm .html
clear cache please!
Jul 16, 2009 @ 06:23
Cpanel = Solutions :).htaccess
AddType application/x-httpd-php4 .htm .html
AddType application/x-httpd-php5 .htm .html
clear cache please!
Ken Said,
AddHandler application/x-httpd-php .php
instead use:
SetHandler application/x-httpd-php
SetHandler application/x-httpd-php-source
using AddHandler enables a scripting attack vector.
Nov 12, 2009 @ 16:31
You shouldn't use:AddHandler application/x-httpd-php .php
instead use:
SetHandler application/x-httpd-php
SetHandler application/x-httpd-php-source
using AddHandler enables a scripting attack vector.
vince Said,
Not quite true.
SetHandler and AddHandler serve different purposes.
AddHandler assigns a handler to a file extension, while SetHandler can only be used with Directory or Location containers.
So your example above is incomplete. If used as provided any file in the directory will be parsed as PHP. You definitely do not want to have your images, css, parsed as PHP
See:
http://httpd.apache.org/docs/1.3/mod/mod_mime.html#sethandler
and
http://httpd.apache.org/docs/1.3/mod/mod_mime.html#addhandler
As for the attack vector, I do not see any difference in terms of security.
SetHandler in combination with location or directory allows you to use more strict way to define what extension to be handled by PHP.
May be you are referring to the latest WordPress exploit?
http://lists.automattic.com/pipermail/wp-hackers/2009-November/028450.html
That can be easily fixed at the Apache level with:
<FilesMatch "\.php$|\.php5$|\.php4$|\.php3$|\.phtml$|\.phpt$">
SetHandler application/x-httpd-php
</FilesMatch>
I guess in this particular case it helps.
Nov 13, 2009 @ 04:00
@KenNot quite true.
SetHandler and AddHandler serve different purposes.
AddHandler assigns a handler to a file extension, while SetHandler can only be used with Directory or Location containers.
So your example above is incomplete. If used as provided any file in the directory will be parsed as PHP. You definitely do not want to have your images, css, parsed as PHP
See:
http://httpd.apache.org/docs/1.3/mod/mod_mime.html#sethandler
and
http://httpd.apache.org/docs/1.3/mod/mod_mime.html#addhandler
As for the attack vector, I do not see any difference in terms of security.
SetHandler in combination with location or directory allows you to use more strict way to define what extension to be handled by PHP.
May be you are referring to the latest WordPress exploit?
http://lists.automattic.com/pipermail/wp-hackers/2009-November/028450.html
That can be easily fixed at the Apache level with:
<FilesMatch "\.php$|\.php5$|\.php4$|\.php3$|\.phtml$|\.phpt$">
SetHandler application/x-httpd-php
</FilesMatch>
I guess in this particular case it helps.
saagar Said,
Jan 06, 2010 @ 08:15
could we specific certain folders in the .htacess to load as .php instead of an entire site?
Ravi Kumar Kangra Said,
I am Ravi
I was warried for 2 days about the same issue using php script into html and after a tremendus amount of search i found the right guidance here as Mr. Oguzhan Basar has explained above the right code is
AddType application/x-httpd-php4 .htm .html
AddType application/x-httpd-php5 .htm .html
for .htaccess configuration to make your .html file run .php scripts.
Thankx to Mr. Orguzhan Basar.
Would like to communicate further....
Ravi Kumar Jangra
Website : Rsuitor.Com
Yahoo Mail : rsuito@yahoo.com
Contact No : +91(0)9811669942.
Feb 25, 2010 @ 12:05
Hello EverybodyI am Ravi
I was warried for 2 days about the same issue using php script into html and after a tremendus amount of search i found the right guidance here as Mr. Oguzhan Basar has explained above the right code is
AddType application/x-httpd-php4 .htm .html
AddType application/x-httpd-php5 .htm .html
for .htaccess configuration to make your .html file run .php scripts.
Thankx to Mr. Orguzhan Basar.
Would like to communicate further....
Ravi Kumar Jangra
Website : Rsuitor.Com
Yahoo Mail : rsuito@yahoo.com
Contact No : +91(0)9811669942.
Binod Shukla Said,
hosting providers do not support they suck....
u guys rock !
Life Savour !
Mar 09, 2010 @ 00:41
Awesome post.hosting providers do not support they suck....
u guys rock !
Life Savour !
| Previous: Block Bad robots, spiders, crawlers and harvesters | Next: Change PHP variables using .htaccess |

I am using a hosting service at hostgator and there
AddHandler application/x-httpd-php .html .htm
seems to work.
Truly
Aditya
www.adiwebs.com