Domanda

I have URL Rewrite setup on an IIS 7.5 site: http://site1.com/

This acts as a reverse proxy to the second site: http://site2.com/

Here is the flow of events:
1. Browser does a GET on http://site1.com/somepath
2. This gets passed through to site2 because site1 is the URL Rewrite reverse proxy. This works well and the host is correctly set because I've done the mod that requires this.
3. site2 responds with a 301 status and sets the HTTP Location header to http://site3.com/somenewpath
4. site1 responds to the browser with a 301 but replaces the host in the Location header with site1: http://site1.com/somenewpath

What I want to happen in step 4 is that site1 responds with http://site3.com/somenewpath in the HTTP Location header and does a straight pass through of this data. I feel that there must be an Outbound rule that can be applied to solve this but haven't been able to figure it out yet.

È stato utile?

Soluzione

Could Application Request Routing be involved? Look at IIS -> Machine or Site -> Application Request Routing Cache -> Server Proxy Settings and uncheck the "Reverse rewrite host in response headers" checkbox. If you do this at the machine level, it'll take effect for all sites. If you do it on a particular site, it'll only take effect for that site, and other sites on the box will be unaffected.

Altri suggerimenti

As I said in the above comments, I believe the default behavior of the reverse proxy is to pass through the response untouched (assumes there are no outbound rewrite rules set). I haven't tested your scenario specifically with a 301 response from the server behind the proxy, though.

If a special outbound rule is in fact needed, this code will modify the HTTP location header of all 301 responses to http://site3.com/somepath

<outboundRules>
  <!-- This rule changes the domain in the HTTP location header for redirect responses -->
  <rule name="Change Location Header">
    <match serverVariable="RESPONSE_LOCATION" pattern="^http://[^/]+/(.*)" />
    <conditions>
      <add input="{RESPONSE_STATUS}" pattern="^301" />
    </conditions>
    <action type="Rewrite" value="http://www.site3.com/{R:1}" />
  </rule>
</outboundRules>

This rule is a slight modification of one posted in URL Rewrite Module 2.0 Configuration Reference

The accepted answer took me in the right direction, but had to do some more digging for newer versions of IIS that do not have the Server Proxy Settings mentioned above.

Credit to these URLs:

IIS AAR - URL Rewrite for reverse proxy - how to send HTTP_HOST

Prevent ARR with UrlRewrite from re-writing Location header for a 302 redirect

On the web server, open the Configuration Editor and go to the path system.WebServer -> proxy. Change the reverseRewriteHostInResponseHeaders to False. This will stop the Location header in the response headers from being rewritten.

The answers that I linked to refers to the preserveHostHeader setting, which did not fix the issue in my case. (My response from the server was 302 with the new Url in the location header)

Before I got to know this perfect solution I used HTML redirecting, which might come in handy if you are not able to change the server settings:

Just store the redirect_url in the <head> of an html document:

<head>
  <meta http-equiv="Refresh" content="0; URL=https://example.com/">
</head>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top