I have added this rule in my web.config to redirect non www URLs to www.
<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>
Although it is working fine for main site URL. Like if user types http://example.com it redirects to http://www.example.com
But for some URLs like
http://www.example.com/userfiles/abc.pdf
it redirects to
http://www.www.example.com/userfiles/abc.pdf
Here you can see 2 time www in URL.