title: IIS 7 Canonical URL redirect
body: <p>I would like to make a website always have www in the address, and enforce it via IIS rewrite.</p>
  
  <p>For example, test.com would become www.test.com.</p>
  
  <p>The typical example rewrite rule is:</p>
  
  <pre><code>&lt;rule name="Canonical Host Name" stopProcessing="true"&gt;
    &lt;match url="(.*)" /&gt;
    &lt;conditions&gt;
      &lt;add input="{HTTP_HOST}" negate="true" pattern="^www\.test\.com$" /&gt;
    &lt;/conditions&gt;
    &lt;action type="Redirect" url="http://www.test.com/{R:1}" redirectType="Permanent" /&gt;
  &lt;/rule&gt;
  </code></pre>
  
  <p>However this requires me to enter the full url of my website. It will not work for development and staging environments that have URLs like www.test.dev and www.test.stage.</p>
  
  <p>Is it possible to create an IIS Rewrite rule that will handle all of those cases?</p>