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 73 >>

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 !
Mike Allbutt Said,
Mar 19, 2010 @ 01:00

After trying many possibilities I finally got this to work with my cpanel set-up that runs PHP Version 5.2.10 in CGI mode.

AddHandler application/x-httpd-php5 .htm .html

Hope that helps someone else.
lee Said,
Mar 22, 2010 @ 10:49

Hi,

Im trying to include a file into a html file but having problems:

in my .htaccess i have:

AddHandler server-parsed .html .htm

Now the problem I have is it works using a html file



BUT NOY USING A PHP FILE



Any ideas why it will add .htm but not .php files in the include?
lee Said,
Mar 22, 2010 @ 11:07

Code working is:

include virtual="footer.htm"

code NOT working is:

include virtual="footer.php"
Keresztes Lehel Said,
Apr 07, 2010 @ 06:02

Thank you all very much. The problem I had is that I didn`t specify exactly that I`m using php5 instead of just php. The script that worked for me was
"AddType application/x-httpd-php4 .htm .html
AddType application/x-httpd-php5 .htm .html"

Cheers
mcitp Said,
May 20, 2010 @ 05:30

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).
vince Said,
May 20, 2010 @ 05:35

@mcitp

You should consult your webhost about the exact .htaccess line you should to use. The

AddHandler application/x-httpd-php .html .htm

is for PHP/CGI based setup. For regular PHP setup you need to use:

AddType application/x-httpd-php5 .htm .html

However, the setting may vary for different hosting providers.
ejaz Said,
May 21, 2010 @ 17:40

Hi.
I've tried all methods and

AddHandler application/x-httpd-php5 .htm .html

works.

P.S. i'm using dreamhost and they seem to be running PHP as fast cgi.
mujji Said,
Jul 05, 2010 @ 02:56

i had the same problem resulting the download of files. but when i use the following code it works fine with me.. hope it will work for u too all..


AddType application/x-httpd-php .html
Baju Bandung Said,
Aug 18, 2010 @ 18:49

@ lee:

i was once facing the same problem with yours: can't include a .php file.
the solution is: include this directive into your .htaccess file:

Options +IncludesNoExec

this file allow execution of the .php file

hope this helps.
Artur Ejsmont Said,
Sep 14, 2010 @ 18:37

there is significant difference in AddType and AddHandler that can cause security risk ... you might want to have a look here

http://artur.ejsmont.org/blog/content/how-apache-phps-setting-exposes-a-security-risk
vince Said,
Sep 15, 2010 @ 03:42

@Artur

Yes, there is a difference - AddType works only when PHP is installed as Module, AddHandler only works with PHP CGI version.

So, If your hosting company is running PHP as a module of Apache, there is not much you can do about it.

The security risk you are talking about is a problem of the application design, not the Apache server or the PHP.

If you allow file uploads, upload the files to a location outside the webroot - e.g not accessible by the apache server directly. Then use a script to read/stream these files when required.
bankole Said,
Sep 29, 2010 @ 19:04

please i ve a .php file dat i would lik 2 rewrite to .phtml
hw do i rewrite frm tutorial.php to
tutorial.phtml


pls hlp!

d hosting service dat i use dnt allow d uploading of .php files, so i heard dat by rewriting it i would b able 2 upload nd futhermore use it.
Athletic gear Said,
Nov 03, 2010 @ 02:52

I have hosted a site in powerhosting and I tried this in my .htaccess but,it is not working....

Is there any other code to make this......

Thanks....
Robc Said,
Dec 10, 2010 @ 18:46

Hi,

This worked on Certified Hosting:

AddHandler application/x-httpd-php5 .html

It allowed me to from an .html file to include php in another .html file.

My own micro-file very simple template engine.
jogeto Said,
Dec 13, 2010 @ 01:31

I am planning to use php includes to clean my pages. Currently all my pages are.One advancement was to change to.test inside php but this will could cause all links to break, including all important seek listings.
roritm Said,
Feb 07, 2011 @ 05:15

how to run php in html file?

---------------
1. - determine which php version running on your server

2. - determine configuration of Apache PHP Handler, is it dso or cgi or SuPHP
dso - mean that php run as Apache module.
cgi or SuPHP - php run as a CGI module.

3. - add to .htaccess

for dso(php as Apache module)

AddType application/x-httpd-php .htm .html

for SuPHP, if your server run php4
AddType application/x-httpd-php4 .htm .html
or
AddType application/x-httpd-php5 .htm .html
if your server run php5

for CGI , add to .htaccess line
AddHandler application/x-httpd-php .html .htm
or
AddHandler application/x-httpd-php5 .html .htm

--------------------------------------

AddType application/x-httpd-php(x) - for php as Apache module, Dso. x - php version
AddHandler application/x-httpd-php(x) - for php as CGI. x - php version
P&P Said,
Feb 14, 2011 @ 08:54

Very useful thanks! i have a important question:

I have a site ranking very well and all files are in .htm, recently i make some changes and i had to add the htaccess file to my server that include this: AddType application/x-httpd-php .htm

I would like to know what exactly this line of code do? the ranking of my website will change? is this code force the browsers to look my files like .php instend of .htm and if yes google crawler do the same?

thanks a lot
P&P
Andrew Said,
Feb 15, 2011 @ 22:06

THis works for cpanel:
AddHandler application/x-httpd-php5 .html .htm
AddType x-mapp-php5 .html .htm
Andr Said,
Feb 18, 2011 @ 04:42

Hello, All,
I have trider all there advices. No one works. If I'm using:
AddType application/x-httpd-php .html. .htm
Server trying to download the page

If I'm using:
AddHandler application/x-httpd-php .html .htm
Nothing happens.

If I'm using:
...php5 - I'm getting the 500 error.

Anyone helps?
Dan Said,
Apr 13, 2011 @ 11:56

I had the same issue after my Host upgraded to WHM 11.28.83 and cPanel 11. All my php includes died.

In my old .htaccess file I had:
"AddHandler application/x-httpd-php .htm .html"

Everything cam back online once I changed it to:
"AddHandler application/x-httpd-php5 .htm .html"

Either they upgraded yo "SuPHP" or changed the method "dso" "Apache Module", I assume they upgraded from dso method to SuPHP. Either way, that solved my issue.
Jason Said,
Apr 17, 2011 @ 15:59

Could someone tell me where I would be able to find a whole .htaccess file to use as an example? I have no idea how to code this in.
vicky Said,
May 18, 2011 @ 14:33

Hello, All,
I have trider all there advices. No one works. If I'm using:
AddType application/x-httpd-php .html. .htm
Server trying to download the page

If I'm using:
AddHandler application/x-httpd-php .html .htm
Nothing happens.

Please Anyone helps?
AJ Said,
Jul 09, 2011 @ 22:46

I did this and it works with WHM and PHP5 for SuPHP.

In the .htacces file:


SetHandler application/x-httpd-php5


If you are doing include files, you will need to change the statement if you have made absolute paths in the past to:



Instead of

Developer Said,
Aug 05, 2011 @ 10:03

AddHandler application/x-httpd-php5 .html

that's what i've searched!! you've saved my day ))))))
Richard Peterson Said,
Aug 07, 2011 @ 20:45

Thanks to all who contributed to this page. I was able to get my local test server and my hosted server to both work with the php/html switch but only after trying out different versions of the above. One likes one thing, the other likes another. To all who are still looking for the solution for their server, I'd say keep trying because there are several different ways it could go.
diseño web Said,
Aug 11, 2011 @ 20:56

I had the same issue after my Host upgraded to WHM 11.28.83 and cPanel 11. All my php includes died.

In my old .htaccess file I had:
"AddHandler application/x-httpd-php .htm .html"

Everything cam back online once I changed it to:
"AddHandler application/x-httpd-php5 .htm .html"

Either they upgraded yo "SuPHP" or changed the method "dso" "Apache Module", I assume they upgraded from dso method to SuPHP. Either way, that solved my issue.
lite_code Said,
Oct 23, 2011 @ 03:56

Hi,
I wonder if this is possible.

see. I have all the files in the root directory, One file is named "services.php" and the other file is named "services1.php" now I want the browser to show the "services1.php" as if it is inside a folder named "services" there is no folder, all the files are in the main directory, I want the browser to show me the "Services1.php" like.

htpp://www.domain.com/services/services1

Please help.
Vince Said,
Oct 23, 2011 @ 08:19

@lite_code

Yes, it is possible

Add the following to your .htaccess file (create the file if you do not have one in your site main directory)

RewriteEngine on
RewriteRule ^services/services1$ /services1.php [L]
lite_code Said,
Oct 23, 2011 @ 08:53

@Vince

Thank you very much for that. However, it is still not working, I have the mod_rewrite enabled and running but still not working. I get a "Not Found Error"

Please advice.
vince Said,
Oct 23, 2011 @ 19:54

@lite_code

Is it possible that you are putting a slash in the URL:

htpp://www.domain.com/services/services1/

If that is the case, please try:

RewriteEngine on
RewriteRule ^services/services1 /services1.php [L]

I just tested it on one of my sites and it worked fine for me
lite_code Said,
Oct 24, 2011 @ 06:26

@vince
Thank you again, Yups! worked this time :), thank you very much..

I wondered if it is possible to do this "Globally". Like if you have 100 files you will need to do the same for all the 100 files or a single line to do it?
vince Said,
Oct 24, 2011 @ 16:34

@lite_code

In general, you can use some regular expressions to group similar rules, for example:

RewriteEngine on
RewriteRule services/services(0-9) /services$1.php [L]

The example above will take care of all requests like:

/services/services1 -> services1.php
/services/services2 -> services2.php

..etc

and even more generic rule:

RewriteEngine on
RewriteRule [a-z]/(a-z)([0-9]+) /$1$2.php [L]

the above might be too general for your case as it will handle all requests in the form of
foldername/forldername1 and as a result may break some other links on your site.

In other words you need to limit the pattern(s) you wish to match, but yes it is possible to handle 100+ files in a few lines of code as long as you define the proper patterns.
lite_code Said,
Oct 25, 2011 @ 03:27

@vince

Thank you very much.. you really helped my a lot, I really appreciate this.
limwell Said,
Oct 26, 2011 @ 00:07

guyz for those who are using the cpanel version and php 4 and php 5. try this code below. hope it helps. i am using this too in my htaccess


AddType application/x-httpd-php4 .htm .html
AddType application/x-httpd-php5 .htm .html
Najam Said,
Dec 22, 2011 @ 06:23

how can i pass variables using .html extension pages???
Teodor Bochev Said,
Jan 03, 2012 @ 02:53

Finally worked, when I added them to CPANEL - Apache Handlers
robertr Said,
Jan 17, 2012 @ 20:54

Finally, I got my website working now. Thanks for the solution.
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