Domains, IIS, Sitecore and SEO
It is often that a site has more than just one domain, and maybe presented with both the prefix with www. and without.
And all the domains point to the same content. Especially if Sitecore is a multi site solution.
For example:
mysite.com
www.mysite.com
mysite.net
www.mysite.net
Maybe even with a other related names like
www.myhomepage.com
and so on.
In the site setup section in Sitecore config file (SiteDefinition.config), the site section might look like this.
All the variant of the domains for the site is piped into hostname proberty.
<sites>
<site name="mysite"
hostname="mysite.com|www.mysite.com"
patch:before="site[@name='website']"
...
/>
</sites>
To make it SEO let it have only one domain to handle and let the others point to it.
What domain is the right one?
Choose the shortest one or the one that is easiest to remember.
Then desire if it should go with www. as prefix or not.
Let the IIS handle all the requests from the different domains before it hits Sitecore.
And let Sitecore have only one set.
<sites>
<site name="mysite"
hostname="www.mysite.com"
patch:before="site[@name='website']"
...
/>
</sites>
That can be done easily with the Url rewrites
Make sure that it is installed on the IIS.
I have collected some rewrites that is good to have.
To add the prefix "www." and still keep the path:
<rewrite>
<rules>
<rule name="Add www." stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.domain.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
To remove the prefix "www." and keep the path:
<rewrite>
<rules>
<rule name="Remove www." stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
To change the domain from one to the another and keep the path:
<rewrite>
<rules>
<rule name="From domain 1 to domain 2" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.domain1.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="http://www.domain2.com/{R:0}" />
</rule>
</rules>
</rewrite>
Force to HTTPS and keep the path:
<rewrite>
<rules>
<rule name="Force to HTTPS" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" redirectType="Found" />
</rule>
</rules>
</rewrite>
NB: Note that the attributes might differ for version to version of the IIS and URL Rewrite. And pattern might not work if "." is not escaped "\."