First create a web.config file in your root directory if it's not already there. Second, put the following code into your web.config file.
Case1: 301 redirect domain.com to www.domain.com
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.com$" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Case2: 301 redirect all domains to one domain name
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Enforce canonical hostname" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.domain\.com$" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Article ID: 300, Created: December 9, 2012 at 7:10 PM, Modified: September 17, 2016 at 8:36 PM