After trying to help a fellow developer on the iis.net forums, I began to search for a better way to do my redirects. And thus I ended up with this article.

I took a lot of these ideas and began to implement my own version of it. Everything seemed to work just okay, but I hit a bump in the road and I hope you can help.

Okay so, a quick explanation on my rules:

My idea is to test the url, and if I find something wrong with it, I rewrite the url and let it go on, rather than redirect it instantly. If at any point I rewrite the url, I set a custom server variable "Redirect" to true. Then in the end, I test if my custom server variable is true and redirect the user.

The point of it is to only have one 301 redirect, rather than a chain of redirects.

These are my rules (Sorry for the wall):

<rules>
  <rule name="WhiteList - resources" stopProcessing="true">
    <match url="^resources/" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
    <action type="None" />
  </rule>

  <rule name="Redirect subdomains with www to non-www" stopProcessing="false">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
      <add input="{HTTP_HOST}" pattern="^www\.(.*)\.([^\.]+)\.([^\.]+)$" />
    </conditions>
    <action type="Rewrite" url="http://{C:1}.{C:2}.{C:3}{HTTP_URL}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>
  <rule name="Redirect top domains with non-www to www" stopProcessing="false">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
      <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
    </conditions>
    <action type="Rewrite" url="http://www.{HTTP_HOST}{HTTP_URL}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>

  <rule name="SEO - Remove trailing slash" stopProcessing="false">
    <match url="(.*)/$" />
    <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>
  <rule name="SEO - ToLower" stopProcessing="false">
    <match url="(.*)" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
      <add input="{URL}" pattern="[A-Z]" ignoreCase="false" />
      <add input="{URL}" pattern="^.*?\.(axd|css|js|jpg|jpeg|png|gif|ashx|asmx|svc).*?$" negate="true" />
      <add input="{URL}" pattern="^.*/(webshop)/.*$" negate="true" />
    </conditions>
    <action type="Rewrite" url="{ToLower:{R:1}}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>
  <rule name="SEO - remove default.aspx" stopProcessing="false">
    <match url="(.*?)/?default\.aspx$" />
    <action type="Rewrite" url="{R:1}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>
  <rule name="SEO - Trim aspx" stopProcessing="false">
    <match url="(.*)\.aspx$" />
    <action type="Rewrite" url="{R:1}" />
    <serverVariables>
      <set name="Redirect" value="true" />
    </serverVariables>
  </rule>

  <rule name="SEO - non-canonical redirect" stopProcessing="true">
    <match url="^(.*)" />
    <conditions>
      <add input="{Redirect}" pattern="true" />
    </conditions>
    <action type="Redirect" url="{R:1}" />
    <serverVariables>
      <set name="Redirect" value="false" />
    </serverVariables>
  </rule>
</rules>

Now most of it actually works pretty well, but I seam to have some issues with a naked domain.

If I take a subdomain with www (which should be redirected to non-www) then it appears to fail. Funny enough, if I go to a subpage, it works fine.

(I can't post more urls because I'm new here :( And I wanted to give credits to the others for starting me on this.)

I've traced it down to the server variable {HTTP_URL} because if I remove it, it doesn't fail. However it of course doesn't do what it is supposed to either (It always redirects to the naked domain). I've tried with various variables: {URL}, {REQUEST_URI} and they all seem to end up with the same error, which is getting a bit annoying.

Should anyone have some improvement for the rules, feel free to respond as well, I love working with them and I would like to make the near perfect redirects, so any suggestions are welcome.


This ended up being more of a version 1 and I have since improved on it to avoid the server variable. Now I use a series of rewrite rules that transforms the url to something I like, appending underscore. Then stripping the underscore and then redirecting. It have worked very well for me for quite some time now and is being used by the company on almost all projects. It have been made into a nuget package for easy setup. http://www.nuget.org/packages/RedirectRules/


I believe I actually found the answer now. I worked around with it for some time and ended up with another issue, again with the www vs non-www. It simply didn't do anything.

So I ended up putting it at the end and making that redirects rather than rewrites as well. Apparently that works fine, why? No idea.

cheesemacfly suggested that I match on ^(.+)$ rather than (.*). I thought it would do the very same thing, but realized that the match isn't on the hostname at all, it is everything after. Big surprise for me there, but it did open up a new possibility:

Instead of matching {URL}, {HTTP_URL} or some other variant of almost the same thing, I could simply refer to the match with {R:1}

I have put in two things since the first post. I put in conditions on some of the rules to exclude them if it contains "umbraco" as I work with that system and most of the rules I don't want to apply to the back-end. I also applied a rewrite map for SSL check, so it automatically puts you to https:// or http:// using the server variable {HTTPS}

So far I haven't had found any bugs in it, so I will mark this as solved and give you my last revision of the rules. Enjoy :)

<rewrite>
    <rules>
        <rule name="WhiteList - resources" stopProcessing="true">
            <match url="^resources/" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="None" />
        </rule>
        <rule name="SEO - Remove trailing slash" stopProcessing="false">
            <match url="(.*)/$" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:1}" />
            <serverVariables>
                <set name="Redirect" value="true" />
            </serverVariables>
        </rule>
        <rule name="SEO - ToLower" stopProcessing="false">
            <match url="(.*)" ignoreCase="false" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{URL}" pattern="[A-Z]" ignoreCase="false" />
                <add input="{URL}" pattern="^.*?\.(axd|css|js|jpg|jpeg|png|gif|ashx|asmx|svc).*?$" negate="true" />
                <add input="{URL}" pattern="^.*/(webshop)/.*$" negate="true" />
                <add input="{URL}" pattern="^/umbraco/" negate="true" />
            </conditions>
            <action type="Rewrite" url="{ToLower:{R:1}}" />
            <serverVariables>
                <set name="Redirect" value="true" />
            </serverVariables>
        </rule>
        <rule name="SEO - remove default.aspx" stopProcessing="false">
            <match url="(.*?)/?default\.aspx$" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{URL}" pattern="^/umbraco/" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:1}" />
            <serverVariables>
                <set name="Redirect" value="true" />
            </serverVariables>
        </rule>
        <rule name="SEO - Trim aspx" stopProcessing="false">
            <match url="(.*)\.aspx$" />
            <action type="Rewrite" url="{R:1}" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{URL}" pattern="^/umbraco/" negate="true" />
            </conditions>
            <serverVariables>
                <set name="Redirect" value="true" />
            </serverVariables>
        </rule>
        <rule name="Redirect subdomains with www to non-www" stopProcessing="false">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
                <add input="{HTTP_HOST}" pattern="^www\.(.*)\.([^\.]+)\.([^\.]+)$" />
            </conditions>
            <action type="Redirect" url="{MapSSL:{HTTPS}}{C:1}.{C:2}.{C:3}/{R:1}" redirectType="Permanent" />
            <serverVariables>
                <set name="Redirect" value="true" />
            </serverVariables>
        </rule>
        <rule name="Redirect top domains with non-www to www" stopProcessing="false">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
                <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
            </conditions>
            <action type="Rewrite" url="{MapSSL:{HTTPS}}www.{HTTP_HOST}/{R:1}" />
            <serverVariables>
                <set name="Redirect" value="true" />
            </serverVariables>
        </rule>
        <rule name="SEO - non-canonical redirect" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{Redirect}" pattern="true" />
            </conditions>
            <action type="Redirect" url="{R:1}" redirectType="Permanent"/>
            <serverVariables>
                <set name="Redirect" value="false" />
            </serverVariables>
        </rule>
    </rules>
    <rewriteMaps>
        <rewriteMap name="MapSSL" defaultValue="OFF">
            <add key="ON" value="https://" />
            <add key="OFF" value="http://" />
        </rewriteMap>
    </rewriteMaps>
</rewrite>

There is a caveat though: Notice that a custom server variable is used. You cannot use a custom server variable out of the box. You need to add this variable to the <allowedServerVariables/> element in the applicationHost.config file, which is a global configuration file for IIS. (Source) If you're using shared hosting (Including Azure Web Sites), chances are that you will not be able to change this setting.

If you're using an Azure Web Role, you'll need to add a startup task that modifies the aforementioned configuration file. If you don't do this, you'll get an IIS 500 error.

If someone have suggestions as to what could make the rules better, they are still welcome.