25
votes

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?

4
Could you define "does not work"? I mean, what actually happens?Rudi Visser
Are you using Application Request Routing? As a test, what if you change type="Rewrite" to type="Redirect"?cheesemacfly
@cheesemacfly yes I am using the Application Request Routing. And when I change it to Redirect it does work and redirect me to the target url. But when using Rewrite I get a 404 error.Kassem
And you have enabled the proxy mode on the Application Request Routing?cheesemacfly
@cheesemacfly: Yes, of course I did.Kassem

4 Answers

53
votes

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/

3
votes

Stumbled across this old post when I was trying to solve the same issue.

SOLVED!

Using Rewrite URL feature in IIS Services Manager I created a friendly URL rule.

This worked ok and when I looked at the rule in the web.config file (www root) it showed 1 rule to redirect and 1 rule to rewrite.

I edited this to suit 1 match. Then I just duplicated this code editing the product ID for each. Example below:

<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
    <match url="^product\.php$" />
    <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^id_product=\b35\b" />
    </conditions>
    <action type="Redirect" url="990mm-bohemia-cast-iron-electric-radiator" 
            appendQueryString="false" />
</rule>

The first rule looks for the string "product.php" in the URL and "id_product=35", it then redirects to "990mm-bohemia-cast-iron-electric-radiator" which currently does not exist. Then (see below)

<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
    <match url="^\b990mm-bohemia-cast-iron-electric-radiator\b" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="product.php?id_product=35" />
</rule>

This rule rewrites the "product.php?id_product=35" bit to `990mm-bohemia-cast-iron-electric-radiator", creating the new location for the redirect.

1
votes

Do make sure MVC routing doesn't steal your request. To prevent that from happening, ignore the route you're trying to rewrite:

RouteTable.Routes.Ignore("blog/{*pathInfo}");

Inspired by: https://sitecore.stackexchange.com/questions/3645/how-to-setup-a-reverse-proxy-with-sitecore

-1
votes

Change the Rewrite URL to AbsolutePath instead putting http://... it should be

<action type="Rewrite" url="{R:1}" /> 

It worked for me, but in my case, I have been rewrite to a fixed webpage.