title: IIS URL Rewrite Module Canonical Link Only If No Subdomain
body: <p>I am using the IIS URL Rewrite module on a Windows 2012 server. I only want to add a canonical www rule if a subdomain is missing (or an exact match on 'example.com').</p>
  
  <p>The problem is the ready-made Canonical rewrite rule IIS creates rewrites all traffic (*) for all subdomains to www.</p>
  
  <pre><code> &lt;rule name="CanonicalHostNameRule1"&gt;
   &lt;match url="(.*)" /&gt;
     &lt;conditions&gt;
        &lt;add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" /&gt;
     &lt;/conditions&gt;
     &lt;action type="Redirect" url="http://www.example.com/{R:1}" /&gt;
   &lt;/rule&gt;
  </code></pre>
  
  <p>The problem is that I have multiple subdomains / applications under one site (and therefore one web.config).</p>
  
  <p>I want</p>
  
  <blockquote>
    <p>example.com</p>
  </blockquote>
  
  <p>to go to</p>
  
  <blockquote>
    <p>www.example.com</p>
  </blockquote>
  
  <p>but to leave these and any other subdomain alone:</p>
  
  <blockquote>
    <p>secure.example.com </p>
    
    <p>profile.example.com</p>
  </blockquote>
  
  <p>Do I need a more specific match regex or pattern?</p>
  
  <p>More specifically, the secure subdomain uses https and a better encryption certificate. </p>