0

I am having a problem using MVC's Redirect method. I am running my MVC4 web application behind a simply IIS proxy. The IIS proxy has one simple rewrite rule that is used so that the site can be accessed under a folder called 'legacy'. The rule looks like this:

proxy.host.com/legacy/{whatever} => host.com:81/{whatever}

However in some situations I need to redirect to domains that are in an entirely different domain where the proxy rules don't apply. I created a simple endpoint that does a redirect to an external domain:

public class ApplicationController
{
    [AllowAnonymous]
    public ActionResult TestRedirect(string returnUrl)
    {
        return this.Redirect("http://AnotherDomain.com/SomeRoute");
    }
}

If I invoke this endpoint like so:

http://proxy.host.com/legacy/Application/TestRedirect

and use a tool like Fiddler to capture the traffic I see the result of calling this endpoint is this:

HTTP/1.1 302 Found
Location: proxy.host.com/Application/SomeRoute
//... Other data omitted for simplicity

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://anotherDomain.com/Application/SomeRoute">here</a>.</h2>
</body></html>

As you can see the body of the response has the expected domain (which was passed in to the Redirect method but the Location header does not contain the same domain. I would expect the location header to contain the domain that was passed into Redirect(). That is, I would expect the response to look like this:

HTTP/1.1 302 Found
Location: http://anotherDomain.com/Application/SomeRoute
//... Other data omitted for simplicity

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://anotherDomain.com/Application/SomeRoute">here</a>.</h2>
</body></html>

I should add that if I turn off the proxy or I bypass it by calling host.com:81/Application/TestRedirect the method works as expected and the response Location header matches the response body.

Can someone please explain why I am getting the unexpected result of the Location header in the response differing from the response body? How can I get the redirect to work as expected?

Thanks.

Rob L
  • 3,073
  • 6
  • 31
  • 61
  • In IIS, do you have Application Request Routing installed? https://www.iis.net/downloads/microsoft/application-request-routing – mjwills Oct 29 '18 at 22:30
  • If you go into ARR and untick `Enable proxy` does the problem stop? Alternatively, if you leave it ticked but untick `Reverse rewrite host in response headers`? – mjwills Oct 30 '18 at 10:22
  • @mjwills When I disabled the Enable proxy check box I ended up getting 404s and other undesired behavior. When I re-enabled Enable proxy but disabled Reverse rewrite host the redirect worked as expected. If you put this in an answer I will accept it. Thanks a ton! – Rob L Oct 30 '18 at 15:01

0 Answers0