IIS Rewrite not working (but redirection does)

Kassem picture Kassem · Mar 12, 2013 · Viewed 21.5k times · Source

I was trying to play with URL re-writing using the Rewrite Module 2.0 but I had no luck getting it to work. What I'm trying to do is re-write all calls to web app at port 80 to other applications hosted in IIS (or maybe on different servers on the network). Using the GUI provided by IIS I created the following rule:

<rewrite>
    <rules>
        <rule name="ReverseProxyInboundRule1" stopProcessing="true">
            <match url="site1/(.*)" />
            <action type="Rewrite" url="http://localhost:7001/{R:1}" />
        </rule>
    </rules>
</rewrite>

Quiet simple, but unfortunately it does not work. On the other hand, when I change the action type to Redirect, it works fine.

What could be the problem?

Answer

Rob Lyndon picture Rob Lyndon · Nov 24, 2015

I ran into this same issue yesterday, and it took me a long time to figure out.

The key here is that you've got an http:// prefix in your rewrite action; that makes this a special case that needs to be handled by Application Request Routing. The first step is to make sure that the Application Request Routing module is installed. You can find the module at https://www.iis.net/downloads/microsoft/application-request-routing. Once that is installed, go to your IIS web server (a level up from your web site), and open the Application Request Routing Cache feature. From the actions on the right, choose Server.Proxy.Settings, and make sure that the "Enable Proxy" checkbox is checked. This allows the URL rewrite task to be re-routed to Application Request Routing, and your reverse proxy should work for external requests.

The idea came from this excellent blog post from 2009: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/