Canonicalize host name to enforce either a www-prefixed domain or non-www-prefixed domain. For SEO it is extremely important to standardize on either the www-prefixed version of the domain or the non-prefixed version and Permanently (301) redirect all requests from the one not in use to the primary one. To not do so would ensure you will suffer duplicate content penalties in search engines.

Enforce www prefix

To canonicalize to a www prefixed domain, add the following rule to web.config’s <rules> section:

<system.webServer>
	<rewrite>
		<rules>
			<rule name="Canonicalize Host Name to www" stopProcessing="true">
			    <match url="(.*)" />
			    <conditions>
			        <add input="{HTTP_HOST}" pattern="^example\.com$" />
			    </conditions>
			    <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" appendQueryString="true" />
			</rule>
		</rules>
	</rewrite>
</system.webServer>

This would cause all requests to http(s)://example.com/ to be redirected to https://www.example.com/. For SEO purposes this redirect should be Permanent (301).

Why Lose Memories?

Enforce non-www prefix

To canonicalize to a non-www prefixed domain, add the following rule to web.config’s <rules> section:

<system.webServer>
	<rewrite>
		<rules>
			<rule name="Canonicalize Host Name to non-www" stopProcessing="true">
			    <match url="(.*)" />
			    <conditions>
			        <add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
			    </conditions>
			    <action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" appendQueryString="true" />
			</rule>
		</rules>
	</rewrite>
</system.webServer>

This would cause all requests to http(s)://www.example.com/ to be redirected to https://example.com/.