How to redirect non www to www

Below is the code you need to enter in to your web.config file in order to redirect a domain to a sub-domain or vice versa.
 
WWW to non WWW:
<rewrite>
  <rules>
    <rule name="Remove WWW prefix" stopProcessing="true">
      <match url="(.*)" ignoreCase="true" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.yourdomain\.com$" />
      </conditions>
      <action type="Redirect" url="http://yourdomain.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
*You will also need to change "yourdomain" to your actual domain on Lines 6 and 8.
 
Non WWW to WWW:
<rewrite>
  <rules>
    <rule name="Add WWW prefix" stopProcessing="true">
      <match url="(.*)" ignoreCase="true" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^yourdomain\.com$" />
      </conditions>
      <action type="Redirect" url="http://www.yourdomain.com/{R:0}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
*You will need to change "yourdomain" to your actual domain on Line 6 and 8.