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

