URL Rewrite keeps original host Location when reverse proxy 301 redirects

20,642

Solution 1

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.

Solution 2

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

Share:
20,642

Related videos on Youtube

Guy
Author by

Guy

I am a consultant specializing in TypeScript/React/JavaScript/Node.js Full Stack. I also have a strong background in C#/.Net. I am available for hire.

Updated on November 17, 2021

Comments

  • Guy
    Guy over 2 years

    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.

  • Steven de Salas
    Steven de Salas over 8 years
    I couldn't get this enabled on a per-site basis (preserveHostHeader=True), only at the ApplicationHost.config level, any ideas why? Ta. Steven
  • DiscipleMichael
    DiscipleMichael about 4 years
    If I could give this 100 extra rep, I would. This saved my butt. I spent hours messing with my reverse proxy url rewrite rules wondering why I had the opposite problem most people complain about (them not being able to revert to the original host). Thanks A million!