colinramsay.co.uk

Rails on IIS - Recognition Failed for despatch.fcgi

15 Mar 2006

I came across this when we were setting up Rails for IIS. To fix it, place the following in request.rb in your rails stuff:

def request_uri
if uri = env['REQUEST_URI']
(%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri # Remove domain, which webrick puts into the request_uri.
else # REQUEST_URI is blank under IIS - get this from PATH_INFO and SCRIPT_NAME
script_filename = env['SCRIPT_NAME'].to_s#.match(%r{[^/]+$})
uri = env['PATH_INFO']

uri = uri.sub("#{script_filename}", "") unless script_filename.nil?

#we must have our rewrite rule look like
#RewriteRule ^(/[^.]*)\?([^.]*)$ /dispatch.fcgi?$1*$2
#It replaces the first '?' with '*'
parts = env['QUERY_STRING'].split("*")

if parts.length > 1
env['QUERY_STRING'] = parts[1]
uri = parts[0]
env['REQUEST_URI'] = uri + '?' + env['QUERY_STRING']
else
#if we didn't have a *real* query string,
#then set the uri to the query string IIS gives us
uri << env['QUERY_STRING'] unless env['QUERY_STRING'].nil?
env['QUERY_STRING'] = ""
env['REQUEST_URI'] = uri
end
uri
end
end

And this in your site httpd.ini:

# Ruby on Rails
IterationLimit 0
RewriteRule ^(/[^.]*)\?([^.]*)$ /dispatch.fcgi?$1*$2RewriteRule ^(/[^.]+)$ /dispatch.fcgi?$1

Thanks to Chris Lang for this.