Ratings, reviews, plans and features to help you find the right web hosting provider for your site.

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

  1. How to block users from accessing your site based on their IP address
  2. How to prevent or allow directory listing?
  3. How to change the error documents – 404 Page Not Found, etc
  4. Using .htaccess for password protecting your folders
  5. Using .htaccess to block referrer spam
  6. Disable Hot-Linking of images and other files
  7. Redirect URLs using .htaccess
  8. Introduction to mod_rewrite and some basic examples
  9. Force SSL/https using .htaccess and mod_rewrite
  10. 301 Permanent redirects for parked domain names
  11. Enable CGI, SSI with .htaccess
  12. How to add Mime-Types using .htaccess
  13. Change default directory page
  14. Block Bad robots, spiders, crawlers and harvesters
  15. Make PHP to work in your HTML files with .htacess
  16. Change PHP variables using .htaccess
  17. HTTP Authentication with PHP running as CGI/SuExec
  18. Force www vs non-www to avoid duplicate content on Google
  19. Duplicate content fix index.html vs / (slash only)

Comments 36 >>

Adi Said,
Oct 05, 2006 @ 14:15

Thank a lot! This was really helpful.

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
Jamie Said,
Oct 14, 2006 @ 09:48

Bit Pointless, as it's security by obscurity. Which always ends up not being worthwhile.
Vince Said,
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,
Nov 10, 2006 @ 06:21

Hi

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.
vince Said,
Nov 10, 2006 @ 17:55

Yes, Jon

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.
Jon Said,
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,
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,
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,
Jul 12, 2007 @ 11:14

Hey Jon & E

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
Kimber Said,
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,
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..
Andre Said,
Jul 01, 2008 @ 12:56

AddType x-mapp-php4 .html .htm

Will that run on PHP 4 only?
Anupam Said,
Jul 11, 2008 @ 02:05

I have tried everything mentioned here and still nothing works. I still get the download option for index.html

Do 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,
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.
mido Said,
Nov 25, 2008 @ 06:46

thanks a lot
it's very useful
Web Design Said,
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,
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.
Pin Said,
Mar 11, 2009 @ 06:01

Thanks. This solves our problem in our cloud hosting too.
dP Said,
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,
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,
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,
Jul 04, 2009 @ 15:04

I added two lines in .htaccess

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.
Tony Said,
Jul 08, 2009 @ 17:10

Can someone please give an explenation to why the file downloads? Thanks.
vince Said,
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,
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,
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,
Nov 13, 2009 @ 04:00

@Ken

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.
Paolo Moro - Business Research srl Said,
Dec 20, 2009 @ 08:31

Thank you for the post, very useful
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,
Feb 25, 2010 @ 12:05

Hello Everybody

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.
Binod Shukla Said,
Mar 09, 2010 @ 00:41

Awesome post.

hosting providers do not support they suck....
u guys rock !
Life Savour !
Your comments on this article

(required)

(required but never displayed)



security code



Previous: Block Bad robots, spiders, crawlers and harvesters Next: Change PHP variables using .htaccess