How do I set the correct Content-Type header for the different browsers?

12,902

mod_setenvif and mod_headers combined should make that possible. I'd suggest setting the normal content type for .html files to application/xhtml+xml if you want to serve that most of the time.

AddType application/xhtml+xml html

Then match the clients you want to treat specially and change the Content-Type:

BrowserMatch some-regex-matching-old-clients is_old_and_crap
Header edit Content-Type application/xhtml\+xml text/html env=is_old_and_crap

Failing that it should be trivial too with mod_rewrite.

RewriteEngine On
RewriteCond %{HTTP:User-Agent} some-regex-matching-old-clients
RewriteRule \.html$ - [T=text/html]

The rule here is used to match requests for files ending with .html which isn't ideal. The extension could appear before the end (such as foo.html.en.gz or not at all (index.html files perhaps). It would be better to use the current value of Content-Type but I believe mod_rewrite runs too early in the request cycle for that to be possible.

Share:
12,902
wbd
Author by

wbd

Updated on June 04, 2022

Comments

  • wbd
    wbd almost 2 years

    I'm running an Apache server to host my websites. They are all written in XHTML5 so I WANT to serve them as application/xhtml+xml.

    The default on Apache is to serve pages as text/html.

    Is there a configuration that I can put at the bottom on my config file that will set the response header Content-Type to application/xhtml+xml for the browsers that support it, otherwise serve as text/html (for < IE9 basically.)