Published by Simon Schoeters

Apache AddHandler or AddType directive?

It took me a while to figure out the differences between the AddHandler and AddType directives in an Apache configuration. Both directives seem to define how the contents of a file with a given extension should be threatened but when do you need the first one and when the second?

Turned out it isn't too hard... the AddType handler specifies how the client (think about a browser) has to deal with a particular data stream (e.g. download a binary or show an image or HTML page). The AddHandler on the other hand maps a handler to the file extension telling the server what to do with it. So if you are defining a Ruby on Rails or PHP content type you will need the AddHandler, telling the server it should parse the file. If you define a JavaScript or HTML file you will need the AddType one to tell the browser what content type to expect.

Strict versus quirks mode

Something slightly related is how browsers actually deal with the content type you set in your AddType directive. Gecko based browsers (like Firefox) follow the standard closely so they will handle a file according to the content type in your Apache configuration. This means your image will show up as text if you wrongly assigned the text/plain content type. Internet Explorer is more flexible and tries to guess the mime type for misconfigured web servers showing a wrongly defined images as, well, an image.

More resources