Converting IIS7 URLRewrite Rules

Topics: User Forum
Sep 19, 2011 at 2:37 PM
Edited Sep 19, 2011 at 2:39 PM


I have successfully installed and appear to running the rewrite filter fine. However I am getting myself into a mess trying to convert some IIS7 rules. Below are the current IIS7 rules I have running;


               <rule name="Site5">

                   <match url=".*" />


                       <add input="{HTTP_HOST}" pattern="^(www.)?$" />

                       <add input="{PATH_INFO}" pattern="^/Sites/5/" negate="true" />


                   <action type="Rewrite" url="\Sites\5\{R:0}" />


               <rule name="Site2">

                   <match url=".*" />


                       <add input="{HTTP_HOST}" pattern="^(www.)?localhost$" />

                       <add input="{PATH_INFO}" pattern="^/Sites/2/" negate="true" />


                   <action type="Rewrite" url="\Sites\2\{R:0}" />



The site I am constructing has multiple domains that run separate home pages for different customers but all run off the same code. Eventually domains will be redirected to e.g.;


For now I am just using my internal and loopback IP addresses to test. The structure of my site is as follows;







Each of the “sites” (i.e. 5 or 2) folders has virtual directory links back to the folders further up the tree. This is needed so when the urlrewrite happens resources can still be found. I have tried many different rules and at the moment am trying the following:


RewriteCond %{HTTP_HOST}           ^(www.))?$

RewriteCond %{PATH_INFO}           !^/Sites/5/$

RedirectRule ^.*       \Sites\5\$1 [L]


This appears to put the browser in a loop I am guessing because as it rewrites to \Sites\5\$1 it then matches the same rule again. Any help would be much appreciated. Hopefully I am fairly close.



Sep 20, 2011 at 12:32 AM

It looks to me like your slashes are going the wrong way.  The target of the rewrite is a URL path.  It is not a path in the IIS metabase. It is not a filesystem path.  The slashes should lean this way /  like in any URL.

The rule you have, redirects to the path.  The condition you use deals with PATH_INFO.  It compares PATH_INFO, to /Sites/5/   The ^ and $ refer to beginning-of-string and end-of-string respectively.  Therefore any URL that is not exactly /Sites/5/   will be rewritten.  A url such as /Sites/5/X will be redirected.  And it will be redirected endlessly.   

You'll need to change your condition, or change your redirect rule, or a combination of those things.