How to Redirect non-www to www domains using Web.config

You can utilize the URL rewrite rules on web.config to redirect user’s requests that has no www to www for your domain name. below is an example of the rewrite rule that you need to inject into your web.config file (after enabling the URL Rewriting feature).

Note: Make sure to replace the domainname word in below XML with your actual site domain name

<rule name=”Redirect to WWW” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^http://domainname.com/$” />
</conditions>
<action type=”Redirect” url=”http://www.domainname.com/{R:0}”  redirectType=”Permanent” />
</rule>