Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts

    Conditionally rewriting mime-types

    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:

    Code:
    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?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Founds this at the w3c list archive; and it works (I've tested it)
    Code:
    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]"

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Beautiful! Oh so beautiful! Thank you sooooo much!

    heh

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No worries

  • #8
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Just tried it out, works a treat!

    Code:
    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)

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #10
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •