Content-Type and Other Headers for HTML


ATTENTION: THIS PAGE IS Valid HTML 5 AND IS BEST VIEWED WITH HTML 5 - Please upgrade your browser or download one of the HTML 5 compatible browsers such as Mozilla Firefox, Chrome, Opera or IE 9 (March 14, 2011 or later). For more information see HTML 5 browsers.

If you find this helpful, please click the Google +1 Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.)

PDF mobile

Content-Type Header

And Other HTTP Headers for HTML

HTTP headers are sent to the client (browser) in the response from the web server before the document itself. The HTTP headers which control how an HTML document is displayed might look like this:

Content-Type: application/xhtml+xml; charset=UTF-8
X-UA-Compatible: IE=9

For static web pages, the headers are provided by the HTTP web server.

When a server-side program or scripting is generating HTML, the language probably has an API to send the proper HTTP headers.

.htaccess Code for Content-Type Header

Set Content-Type Header using .htaccess AddType

For static web pages, it may be necessary to add the MIME Type for HTML to the HTTP web server configuration to send the appropriate Content-Type header. With the Apache HTTP Server, for example, the HTML 5 MIME Type can be added to the .htaccess file(s):

AddType application/xhtml+xml;charset=UTF-8 html

For a detailed explanation of why the Content-Type header is set to application/xhtml+xml, see HTML 5's HTML Serialization.

It is highly recommended that the charset attribute specifying the character encoding of the HTML page be included in the Content-Type header for non-XML user agents as well as in the xml declaration for XML parsers.

HTTP Header Examples

Examples of HTTP Headers for HTML
DirectoryIndex index.html
ErrorDocument 404 /error.html
AddType application/xhtml+xml; charset=UTF-8 html

In this example, the DirectoryIndex document comes from the directory indicated by the HTTP request URL and the ErrorDocument comes from the document root directory, due to the leading slash (/) in that directive. The Content-Type header for any .html documents will have a value of application/xhtml+xml and the documents will be encoded with UTF-8 character encoding.

Valid HTML 5