homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Serving PHP with Apache: AddType and AddHandler
Can anyone explain the differences in php options to AddType & AddHandler?

 9:44 pm on Mar 21, 2013 (gmt 0)


I have a question about what seem to be stark differences in documentation about what options/arguments should be provided for the AddType and AddHandler directives in Apache to properly configure PHP to display as HTML to a browser (when misconfigured it may display the actual PHP source code to a browser).

First of all there appear to be different ways to serve up PHP pages (I assume PHP5 for sake of this post) on an Apache server, the biggest two being to A) use the Apache PHP module (on RHEL/CentOS: "LoadModule php5_module /usr/lib64/httpd/modules/libphp5.so") or B) tell Apache to associate PHP with CGI/make it's handler CGI.

That may have some bearing on the differences I see, but here is my main question:

In many Wordpress, Joomla, and other PHP based CMS forums I see recommendations to add this to one's Apache config so that PHP renders correctly:

AddType application/x-httpd-php .php

However for some browsers this causes a problem (Firefox in a few cases will open a file dialogue box asking what program should be used to display a file of type "application/x-httpd-php" - perhaps only older versions of Firefox?) because it does not understand the MIME type "application/x-httpd-php". In those cases most people suggest a "safer" config of:

AddType text/html .php

Which assures that the browser understands that it is HTML (the PHP should already have been pre-processed by the server and converted into pure HTML before it sends it to the browser anyway, right?). Nonetheless, I see suggestions in articles and forums persist on recommending the setting:

AddType application/x-httpd-php .php

Why is that, and why would you want to set it to anything other than a "text/html" type, especially if the alternate MIME type causes problems?


Separately, for server-side processing of the PHP I also see some suggestions for "AddHandler" to be set as:

AddHandler application/x-httpd-php .php

But others as:

AddHandler php5-script .php

What is the difference? Is one used when you are using the Apache PHP module but the other if you are using CGI to run PHP?

Personally my configuration (as setup by default on CentOS in /etc/httpd/conf.d/php.conf which is created by running 'yum install php') uses the Apache PHP module (not a CGI handler - unless I have misunderstood/misconfigured something) and those two Apache directives are set to:

AddHandler php5-script .php
AddType text/html .php

And I have no "x-httpd-php" settings for either. So what is the need for "x-httpd-php", especially if it causes problems for some browsers (and frequently encountered with certain Wordpress installations it seems)?



 10:08 pm on Mar 21, 2013 (gmt 0)

Also, as a reference, this has been partially discussed on this forum before in an older thread I found here: [webmasterworld.com...]

However it seems that the issue remained unresolved and someone even wrote:

"The AddType / AddHandler stuff is beyond confusing and I usually end up trying several combinations before I find the one that works OK."

Which is a very defeatist attitude IMO (I say this partly tounge-in-cheek), and implies there is no explanation. There has to be an explanation! :-)

In that thread SteveWh also implies that there is a difference in how (strictly according to the Apache documentation) the directives are supposed to work and how they actually work (also unsatisfying - and confusing).


 1:31 pm on Mar 22, 2013 (gmt 0)

welcome to WebmasterWorld, jmnielsen7!

the answer is - it depends.
depending on how your particular server is configured and the order modules are processed and ...

look at jdMorgan's responses in this thread and it kind of explains why there is not one correct answer and why some of the "documented solutions" don't work.
browser asking me to download php files - Apache Web Server forum:
http://www.webmasterworld.com/apache/3418477.htm [webmasterworld.com]


 2:23 pm on Mar 22, 2013 (gmt 0)

AddType principally adds a content type header to the HTML page that is output.

AddHandler principally tells Apache to invoke a PHP handler for .php files. There are several ways of doing that, hence several variations in the syntax.

The functionality and the syntax has also varied between Apache 1.3, 2.2 and 2.4.

As I can never remember the "right way" for each version, and what you need to do can also depend on other server settings set by the host, it often is a case of trying different things until it works.


 2:43 pm on Mar 22, 2013 (gmt 0)

Thanks phranque!

It figures that there wouldn't be any cut-and-dry answer that fits all cases. Although it does indeed seem related (in part) to whether one is processing PHP with a module or with CGI. I actually did find jdMorgan's responses quite helpful and found this explanation enlightening:

"The theoretically-correct way to tell Apache to execute a script is to use the AddHandler directive. However, due to a kludge in the way PHP was first implemented, the seemingly-nonsensical application of the AddType directive is sometimes required. As I understand it, whether you need to use AddHandler or AddType depends on whether your PHP is installed as a module or as CGI."

And also:

"You're also fighting "institutionalized" confusion between the very-different functions of AddType (which defines the MIME-type header to be returned with content to the client), and AddHandler, which tells the server how to handle each filetype in the server's filespace.

The examples posted above demonstrate this "institutionalized" confusion; Although the use "AddHandler application/x-httpd-php .php" to declare a MIME-type as a content-handler is glaringly nonsensical, the fact is that it will work on some servers."

"Institutionalized confusion" eh? Ah well, I suppose to eat my own dog food (my own terminology) I too must call myself a 'defeatist' and succumb to the "institutionalized confusion" as well. Doh!

Thanks again,


 3:03 pm on Mar 22, 2013 (gmt 0)

Hi g1smd,

AddType principally adds a content type header to the HTML page that is output.

AddHandler principally tells Apache to invoke a PHP handler for .php files. There are several ways of doing that, hence several variations in the syntax.

The functionality and the syntax has also varied between Apache 1.3, 2.2 and 2.4.

As I can never remember the "right way" for each version, and what you need to do can also depend on other server settings set by the host, it often is a case of trying different things until it works.

Thanks for this clarification as well. I read the Apache documentation and got a sense for what the AddType and AddHandler directives are supposed to do, but as pointed out in the quotes above sometimes those directives have to be used in seemingly nonsensical ways to get a particular configuration working. After reading almost 20 articles and forum pages on this topic I began to suspect that some of the variety in the ways of doing it also depended on the particular Handler/Interpreter that one was using server-side for PHP.

According to this website [blog.servint.net] some of the common PHP handlers are: DSO (mod_php, a.k.a the integrated Apache PHP module), CGI, FastCGI, and suPHP.

However since I am using an "all defaults" installation on RHEL/CentOS by just installing all the necessary yum packages and then double-checking the apache config ('yum install php' gives you /etc/httpd/conf.d/php.conf, which utilizes the Apache module it looks like, if that's what the php5-script handler is), therefore I'm not even messing with the "alternative" ways of running it such as through CGI or suPHP. I essentially want to know (for my purposes) what the "standard" settings for AddHandler and AddType are when only utilizing Apache's own PHP module.

[edited by: jmnielsen7 at 3:43 pm (utc) on Mar 22, 2013]


 3:15 pm on Mar 22, 2013 (gmt 0)

Try the usual methods in turn, and when it works, you'll know. :)


 3:33 pm on Mar 22, 2013 (gmt 0)

Thanks again. One last thing, although it is slightly off-topic. Is there a way (if I were curious as to the inner workings of how the Handlers are managed) to run Apache in some a verbose tracing mode to see every module call that is made when it is used? Even the LogLevel 'debug' does not give that much detail.

For example say I have several modules loaded into Apache that can potentially interpret PHP (even the CGI has a module that Apache uses to "outsource" the action doesn't it?) but say I don't know which handler-names are tied to which modules. The name "LoadModule php5_module" is not identical to "AddHandler php5-script", so aside from explicit documentation (which may exist - haven't looked) I merely assume that the php5-script Handler name utilizes the php5_module that I loaded and not some other candidate module which I may have also loaded.

How could I "scientifically" observe which module is called/invoked in the background when I use "AddHandler php5-script"? Is that even possible without heavily mod-ing Apache to give you an "internals" or tracer view?



 4:05 pm on Mar 22, 2013 (gmt 0)

starting with (i think) apache 2.4, per-module logging was implemented but if you leave out the module identifier the LogLevel applies to all modules.
on a live server, i would recommend implementing this with care as it could adversely effect the efficiency of some modules.


 5:36 pm on Mar 22, 2013 (gmt 0)

***Relevant to the OP!***


After a bit more digging I discovered how "application/x-httpd-php" (a MIME type - as jdMorgan pointed out) can be used as a Handler, and honestly it seems a bit like slight-of-hand. I would have to say in a somewhat *unfortunate* overlap of naming, the Apache Action directive allows a CGI script to be mapped directly to a MIME media type name as a handler. Then once defined/mapped with an Action directive that Action-name (it no longer refers to the MIME type in the syntactical context of the AddHandler directive but rather to a mapped Action-name) can be called by AddHandler as a user-defined handler name. Also Action can define any arbitrary string name which can equally be used by AddHandler.

My first pointer was from a Google Books preview of the book "Pro Apache" on page 350 here [books.google.com] where it says:

The Action directive allows CGI scripts to be used as handlers. It has two variants: associating a script with either a media type or an arbitrary name, which can then be used by the AddHandler and SetHandler directives.

Apache understands several different MIME types for differentiating various kinds of media. You can add your own handler to process one of them by using the Action directive with a MIME-type parameter:

Action image/gif /cgi-bin/gifconvert.cgi

You can invent new media types or supplement existing ones using AddType in combination with Action, but this approach is deprecated in favor of using AddHandler or SetHandler. For example, to allow a different extension for SSIs, you could use this:

AddType application/x-server-parsed .ssi

or to invent a completely new media type and have it parsed with your own handler, you could use this:

Action text/my_text_type /cgi-bin/my_text_type_handler
AddType text/my_text_type .mytext

In addition to the internal handlers provided by Apache modules, you can also define your own handlers with the Action directive. This is similar to using Action with MIME types but with an arbitrary handler name:

Action my_handler /cgi-bin/my_handler
AddHandler my_handler .mytext

This provides an excellent way to hide CGI scripts behind seemingly ordinary URLs; the handler can be associated with a directory, and then any file request in that directory will instead go to the handler."

Okay, so how is this relevant to the question in the OP about what AddHandler application/x-httpd-php .php does? As jdMorgan pointed out (in the quotes I made in an earlier post) it APPEARS that "the use 'AddHandler application/x-httpd-php .php' to declare a MIME-type as a content-handler is glaringly nonsensical", but the fact is that it actually is the result of AddHandler being paired with a corresponding Action directive (which must be present or it will never work!), which ties the MIME type media name to a CGI script and provides that name as a new handler name. And it is a little unclear but it seems that if a MIME type is not by default defined or known to Apache you can use AddType to define one ("invent a completely new media type") and then use it with Action. So AddHandler and Action may or may not also need to be paired with AddType (try without it first though to avoid unnecessary issues).

And once I discovered the above information, lo and behold, what do I find on the Apache wiki for PHP4/PHP5 [wiki.apache.org]? I find instructions to add the following to httpd.conf:

LoadModule php5_module modules/libphp5.so
AddHandler application/x-httpd-php .php .phtml
Action application/x-httpd-php modules/libphp5.so

So the "Action application/x-httpd-php modules/libphp5.so" directive here was the missing piece to the puzzle all along! This is probably why so many Joomla and Wordpress configurations were messed up when people were complaining about "AddHandler application/x-httpd-php .php", because they had no corresponding Action directive that mapped the MIME media type name to a CGI script (they were calling an undefined handler name). So Voilą! Mystery solved.



 6:13 pm on Mar 22, 2013 (gmt 0)

Also note the examples on this page as well: MapScript and PHP as a CGI [trac.osgeo.org].

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved