« TPAC 2007 - W3C meets the Web community | Main | HTML WG in Cambridge, USA - 8-10 November 2007 »

Setting media type headers on your Web site

On the Web, no one knows you're a dog
... unless you are served with the proper HTTP headers.

What makes a CSS stylesheet a CSS stylesheet, and not just a text file that happens to look a lot like CSS? What may sound like an abstract philosophical reflection on the ontological nature of Web resources, became a very practical issue around 2002, when one of the major Web Browsers was patched to take HTTP information seriously, and would ignore stylesheets unless they were properly served as such by the web servers.

To understand what happens, one needs to look into the process of delivering resources on the Web: whenever a resource is requested, and served, by a Web server, it isn't sent on its own: the server prepends a lot of important information in the HTTP Headers. That information can be about the size of the representation served, caching directives, character encoding. And, among the most important: the media type. This is where the server tells the client "please treat this as a jpeg image/css stylesheet/xml document". That information is authoritative, and if a server declares "this is text/plain", clients have to treat content as plain text, even if it happens to look like HTML, CSS, or a PNG image. Guessing against this information is considered a bad idea.

The importance of this information provided by the Content-Type HTTP header means that one may sometimes want to set, or force, the served media type for a resource, or many. Let's look at a few techniques for a number of practical cases.

Static resources: set up a web server, leverage its defaults

Many resources on the web are "static", that is, they are equivalent to files sitting on, and served by the Web server.

For those, the most simple way to set up the media type served by the server is to use a recognized file extension. For instance, a file with extension .css will automatically be served as text/css, files with extension .html and .xhtml will automatically trigger content-types text/html and application/xhtml+xml, etc.

Sometimes, however, one may want to force a specific mime type for a resource, regardless of what the static file is, or add a media type to the configuration of the Web server. For instance, we recommended in the Web Standards Do that one may want to server XHTML files as text/html for public consumption (at least for as long as XML support in generic browser remains weak), but use the stricter parsing of application/xhtml+xml for testing purposes. For such cases, each Web server has a different way of configuring the media types:

Dynamic Scripting Languages

Not all Web resources are static, and a growing number of them are actually generated by "CGI" or dynamic scripting engines. Below are examples of setting the Content-Type header with such languages.

If your favorite language is not listed, feel free to share your techniques in the comments below.

Filed by olivier Théreaux on October 4, 2007 1:46 AM in HTTP
| | Comments (6) | TrackBacks (0)

Comments

Asbjørn Ulsberg # 2007-10-04

Trying to sign in via OpenID yields the following error: "Can't locate XML/Parser.pm in @INC". Anyway, I just wanted to share how to set the content type in ASP and ASP.NET:

Response.ContentType = "text/html"

If you're using C# in ASP.NET, just add a semicolon at the end of the statement. For a reference, http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.contenttype.aspx will do.

olivier # 2007-10-04

Thank you Asbjørn, I am bumping your notes up into the article.

And apologies indeed for the issues with openid signup. I did fix the perl library issue you ran into, but the system remains flaky...

Asbjørn Ulsberg # 2007-10-05

Now, when I sign in with OpenID, I'm just redirected back here like nothing happened. E.g. I don't look signed in and still have to use the "comment anonymously" option. Thanks for the bump, by the way.

Anthony Ettinger # 2007-10-10

I found several cases in the last few years where the content-type in the html doc did not override the content-type in the header.

This was with charset specifically.

olivier Théreaux Author Profile Page # 2007-10-10

karl dubost, w3c # 2007-10-11

@Antony: if you want to see how it is working and if you are using Apache for your own server, you can do this. In a .htaccess file, you can ask the server to not send any type information for the file foo.html

<Files foo.html>
ForceType None
</Files>

Then the meta will take over, because the http header information will be empty.

Leave a comment

Note: this blog is intended to foster a polite, on-topic and interesting discussion. Comments failing these requirements and spam will not get published; others will appear on the entry page after review by the staff. This may take some time: thank you for your patience.

Your comment


About you

This blog is written by W3C staff and working group participants,
 and maintained by Karl Dubost and olivier Thereaux.
Powered by Movable Type, magpierss and a lot of Web Technology