It is good practice for a website to have a single canonical URL that is always used when accessing the site.
Typically there may be one or more staging URL used during development and the client may have also registered both the www and non-www versions of their website URL.
Choose one URL to be the canonical URL and add one of the two versions of the following rewrite rules to the site web.config
1. Canonical rule for a website running over HTTP
<rule name="Enforce canonical hostname" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^mywebsite\.com$" />
</conditions>
<action type="Redirect" url="http://mywebsite.com/{R:1}" redirectType="Permanent" />
</rule>
2. Canonical rule for a website running over HTTPS
This rule also forces the site to run over HTTPS. Note that you must have installed a valid SSL certificate for the site.
<rule name="Enforce canonical hostname" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://mywebsite.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Enforce canonical hostname ssl" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^mywebsite\.com$" />
</conditions>
<action type="Redirect" url="https://mywebsite.com/{R:1}" redirectType="Permanent" />
</rule>