Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
MIddleware for making legacy versions of Internet Explorer (anything below 11) work with rails. I release this code under MIT license.
# ./config/initializers/rewrite_ie_header.rb
# Don't forget to include the middleware file! If you have a ./lib/middleware folder in rails
# a `require './lib/middleware/rewrite_ie_header.rb'` should do the trick
# IE has a really stupid accept header, so here we scope its domain of mime-types that it can negotiate, otherwise we'll
# end up with weird bugs where if you hit /mcp/:id, IE will download a .ppt file instead of the .html content.
# Issue documented in more detail at
Rails.application.config.middleware.use RewriteIEAcceptHeader do |ie_mime_types|
ie_mime_types.register Mime::HTML
ie_mime_types.register Mime::JSON
ie_mime_types.register Mime::ALL
# IE pukes a bunch of mime-types when we post forms at the server in a random order.
# so basically you can't count on those headers.
class RewriteIEAcceptHeader
def initialize(app)
@app = app
yield self
def call(env)
if crappy_browser?(env) && (mime_type = detect_mime_type(env))
env["HTTP_ACCEPT"] = mime_type.to_s
def register(mime_type)
mime_types << mime_type
def mime_types
@mime_types ||= []
def detect_mime_type(env)
# PPT Presenter ajax calls don't have an ACCEPT header. However still have a MSIE mimetype.
# Old Mime::Type in rails2 didn't care that this was nil. In Rails3 it explodes.
Mime::Type.parse(env["HTTP_ACCEPT"] || "").find{ |m| mime_types.include?(m) }
# Detect if the user agent is IE trying to put or post...
def crappy_browser?(env)
env["HTTP_USER_AGENT"] =~ /msie/i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment