title: One redirect to rule them all
body: <p>After trying to <a href="http://forums.iis.net/t/1195459.aspx/1?Going+crazy+with+trailing+slash+and+double+redirects" rel="nofollow noreferrer">help a fellow developer</a> on the iis.net forums, I began to search for a better way to do my redirects. And thus I ended up with <a href="http://www.seomoz.org/blog/what-every-seo-should-know-about-iis#chaining" rel="nofollow noreferrer">this article.</a></p>
  <p>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.</p>
  <p>Okay so, a quick explanation on my rules:</p>
  <p>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 &quot;Redirect&quot; to true. Then in the end, I test if my custom server variable is true and redirect the user.</p>
  <p>The point of it is to only have one 301 redirect, rather than a chain of redirects.</p>
  <p>These are my rules (Sorry for the wall):</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;rules&gt;
    &lt;rule name=&quot;WhiteList - resources&quot; stopProcessing=&quot;true&quot;&gt;
      &lt;match url=&quot;^resources/&quot; /&gt;
      &lt;conditions logicalGrouping=&quot;MatchAll&quot; trackAllCaptures=&quot;false&quot; /&gt;
      &lt;action type=&quot;None&quot; /&gt;
    &lt;/rule&gt;
  
    &lt;rule name=&quot;Redirect subdomains with www to non-www&quot; stopProcessing=&quot;false&quot;&gt;
      &lt;match url=&quot;(.*)&quot; /&gt;
      &lt;conditions logicalGrouping=&quot;MatchAll&quot; trackAllCaptures=&quot;false&quot;&gt;
        &lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;.*localhost.*&quot; negate=&quot;true&quot; /&gt;
        &lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;^www\.(.*)\.([^\.]+)\.([^\.]+)$&quot; /&gt;
      &lt;/conditions&gt;
      &lt;action type=&quot;Rewrite&quot; url=&quot;http://{C:1}.{C:2}.{C:3}{HTTP_URL}&quot; /&gt;
      &lt;serverVariables&gt;
        &lt;set name=&quot;Redirect&quot; value=&quot;true&quot; /&gt;
      &lt;/serverVariables&gt;
    &lt;/rule&gt;
    &lt;rule name=&quot;Redirect top domains with non-www to www&quot; stopProcessing=&quot;false&quot;&gt;
      &lt;match url=&quot;(.*)&quot; /&gt;
      &lt;conditions logicalGrouping=&quot;MatchAll&quot; trackAllCaptures=&quot;false&quot;&gt;
        &lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;.*localhost.*&quot; negate=&quot;true&quot; /&gt;
        &lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;^([^\.]+)\.([^\.]+)$&quot; /&gt;
      &lt;/conditions&gt;
      &lt;action type=&quot;Rewrite&quot; url=&quot;http://www.{HTTP_HOST}{HTTP_URL}&quot; /&gt;
      &lt;serverVariables&gt;
        &lt;set name=&quot;Redirect&quot; value=&quot;true&quot; /&gt;
      &lt;/serverVariables&gt;
    &lt;/rule&gt;
  
    &lt;rule name=&quot;SEO - Remove trailing slash&quot; stopProcessing=&quot;false&quot;&gt;
      &lt;match url=&quot;(.*)/$&quot; /&gt;
      &lt;conditions&gt;
        &lt;add input=&quot;{REQUEST_FILENAME}&quot; matchType=&quot;IsFile&quot; negate=&quot;true&quot; /&gt;
        &lt;add input=&quot;{REQUEST_FILENAME}&quot; matchType=&quot;IsDirectory&quot; negate=&quot;true&quot; /&gt;
      &lt;/conditions&gt;
      &lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}&quot; /&gt;
      &lt;serverVariables&gt;
        &lt;set name=&quot;Redirect&quot; value=&quot;true&quot; /&gt;
      &lt;/serverVariables&gt;
    &lt;/rule&gt;
    &lt;rule name=&quot;SEO - ToLower&quot; stopProcessing=&quot;false&quot;&gt;
      &lt;match url=&quot;(.*)&quot; ignoreCase=&quot;false&quot; /&gt;
      &lt;conditions logicalGrouping=&quot;MatchAll&quot; trackAllCaptures=&quot;false&quot;&gt;
        &lt;add input=&quot;{URL}&quot; pattern=&quot;[A-Z]&quot; ignoreCase=&quot;false&quot; /&gt;
        &lt;add input=&quot;{URL}&quot; pattern=&quot;^.*?\.(axd|css|js|jpg|jpeg|png|gif|ashx|asmx|svc).*?$&quot; negate=&quot;true&quot; /&gt;
        &lt;add input=&quot;{URL}&quot; pattern=&quot;^.*/(webshop)/.*$&quot; negate=&quot;true&quot; /&gt;
      &lt;/conditions&gt;
      &lt;action type=&quot;Rewrite&quot; url=&quot;{ToLower:{R:1}}&quot; /&gt;
      &lt;serverVariables&gt;
        &lt;set name=&quot;Redirect&quot; value=&quot;true&quot; /&gt;
      &lt;/serverVariables&gt;
    &lt;/rule&gt;
    &lt;rule name=&quot;SEO - remove default.aspx&quot; stopProcessing=&quot;false&quot;&gt;
      &lt;match url=&quot;(.*?)/?default\.aspx$&quot; /&gt;
      &lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}&quot; /&gt;
      &lt;serverVariables&gt;
        &lt;set name=&quot;Redirect&quot; value=&quot;true&quot; /&gt;
      &lt;/serverVariables&gt;
    &lt;/rule&gt;
    &lt;rule name=&quot;SEO - Trim aspx&quot; stopProcessing=&quot;false&quot;&gt;
      &lt;match url=&quot;(.*)\.aspx$&quot; /&gt;
      &lt;action type=&quot;Rewrite&quot; url=&quot;{R:1}&quot; /&gt;
      &lt;serverVariables&gt;
        &lt;set name=&quot;Redirect&quot; value=&quot;true&quot; /&gt;
      &lt;/serverVariables&gt;
    &lt;/rule&gt;
  
    &lt;rule name=&quot;SEO - non-canonical redirect&quot; stopProcessing=&quot;true&quot;&gt;
      &lt;match url=&quot;^(.*)&quot; /&gt;
      &lt;conditions&gt;
        &lt;add input=&quot;{Redirect}&quot; pattern=&quot;true&quot; /&gt;
      &lt;/conditions&gt;
      &lt;action type=&quot;Redirect&quot; url=&quot;{R:1}&quot; /&gt;
      &lt;serverVariables&gt;
        &lt;set name=&quot;Redirect&quot; value=&quot;false&quot; /&gt;
      &lt;/serverVariables&gt;
    &lt;/rule&gt;
  &lt;/rules&gt;
  </code></pre>
  <p>Now most of it actually works pretty well, but I seam to have some issues with a naked domain.</p>
  <p>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.</p>
  <p>(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.)</p>
  <p>I've traced it down to the server variable <code>{HTTP_URL}</code> 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: <code>{URL}</code>, <code>{REQUEST_URI}</code> and they all seem to end up with the same error, which is getting a bit annoying.</p>
  <p>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.</p>