PDA

View Full Version : Conditionally rewriting mime-types



jkd
11-25-2002, 06:32 AM
I am on an Apache web server with mod_rewrite installed.

I am also extensively using correct XHTML which requires serving it as application/xhtml+xml (or another XML mime-type) instead of text/html.

Internet Explorer however is pretty stupid and doesn't recognize the mime-type, and therefore tries to download all of my pages.

To get around this, I basically want to scan all requests for "MSIE" in their use-agent, and if found rewrite the mime-type to text/html.

Unfortunately:



RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} MSIE
RewriteRule ^(.*)\.xhtml$ $1.xhtml [type=text/html]


Doesn't seem to do anything my .htaccess file? Any ideas, or other methods? :)

brothercake
11-26-2002, 10:18 PM
Well, I was going to comment ... but can't make it work either. Instinctively, it feels like there has to be a solution for something so useful. I'm gonna mull on it.

jkd
11-26-2002, 11:19 PM
Thinking about it, the problem may have to do with mime-type second-guessing by Internet Explorer.

I've heard that it sometimes overrides server mime-types by looking at the extension...

I'm going to experiment more with in a bit, but if that's the case, you'd have to save everything as .html, then serve as application/xhtml+xml to non MSIE browsers, OR rewrite the url to:

RewriteRule ^(.*)\.xhtml$ $1.xhtml?hack=.html [type=text/html]

I can't test it right now, but later tonight I should be able to say something about it.

whammy
12-17-2002, 12:42 AM
Just going by what someone else said... but have you tried setting the MIME type to "unknown"? Something works like that in ASP (I've seen it working), but I'm not sure of the syntax - I am actually going to research it a bit in the coming week, since a client with a potential moneymaker for our company had this requirement:

'When links to .pdf files are clicked, use the Windows standard "Open Mail Application" protocol which gives the user the option to download or open in the browser'...

Heheheh. I'm still trying to figure out what the "OMAP" is, though. ;)

brothercake
03-22-2003, 10:53 PM
Founds this at the w3c list archive (http://lists.w3.org/Archives/Public/www-archive/2002Dec/0005.html); and it works (I've tested it) :)


AddType application/xhtml+xml html

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} (text/html|\*/\*)
RewriteCond %{REQUEST_FILENAME} .*\.html
RewriteRule ^.*$ - "[T=text/html,L]"

jkd
03-23-2003, 12:25 AM
Beautiful! Oh so beautiful! Thank you sooooo much! :)

heh :D

brothercake
03-23-2003, 12:33 AM
No worries :)

jkd
03-23-2003, 12:42 AM
Just tried it out, works a treat!



RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} (text/html|\*/\*)
RewriteCond %{REQUEST_FILENAME} .*\.xhtml
RewriteRule ^.*$ - "[T=text/html,L]"


Is how I put it to use... if I have a file saved as .html, I probably intend for it to be served as text/html, so I just use the .xhtml extension as the variable one. (which makes sense anyway) :)

brothercake
03-23-2003, 12:58 AM
Oh wow ... and IE handles it fine because of the mime-type, even though it has that 'alien' file extension?

Who says you can't teach an old dog new tricks ;)

jkd
03-23-2003, 01:20 AM
Originally posted by brothercake
Oh wow ... and IE handles it fine because of the mime-type, even though it has that 'alien' file extension?

I'm a little surprised too... IE's mime-type second guessing is a real pain in the butt (ever try dynamically serving SVG?), but I guess it "prefers" text/html over extensions, which for once is what it is supposed to do.