The following code sample will 301-redirect http://domainname.com to http://www.domainname.com with the following requirements:
1. IIS v7 (Windows 2008) or newer
2. The site's application pool runs in "integrated" mode
3. The URL Rewrite module is installed
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\." ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}{URL}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>