title: web.config redirect non-www to www
body: <p>I need to redirect non-www URLs to www URL  for both HTTP and HTTPS URLs. I tried following rules in <code>web.config</code>.</p>
  <pre><code>&lt;rule name=&quot;Redirect to WWW&quot; stopProcessing=&quot;true&quot;&gt;
      &lt;match url=&quot;.*&quot; /&gt;
      &lt;conditions&gt;
          &lt;add input=&quot;{HTTP_HOST}&quot; pattern=&quot;^example.com$&quot; /&gt;
      &lt;/conditions&gt;
      &lt;action type=&quot;Redirect&quot; url=&quot;http://www.example.com/{R:0}&quot; redirectType=&quot;Permanent&quot; /&gt;
  &lt;/rule&gt;
  
  &lt;rule name=&quot;Redirect to WWW https&quot; stopProcessing=&quot;true&quot;&gt;
      &lt;match url=&quot;.*&quot; /&gt;
      &lt;conditions&gt;
          &lt;add input=&quot;{HTTPS}&quot; pattern=&quot;^example.com$&quot; /&gt;
      &lt;/conditions&gt;
      &lt;action type=&quot;Redirect&quot; url=&quot;https://www.example.com/{R:0}&quot; redirectType=&quot;Permanent&quot; /&gt;
  &lt;/rule&gt;
  </code></pre>
  <p>It works perfectly for non-ssl URL but in case of SSL it redirect from <code>https://example.com</code>  to <code>http://www.example.com</code>.</p>