Now that we’ve saved the rule, let’s try it out on our
site. Try the following two URLs on my site:
http://scottgu.com
http://scottgu.com/
Notice that the first URL (which has no trailing slash)
automatically does a redirect to a URL with the trailing slash. Because
it is a permanent redirect, search engines will follow the URL and update the
page ranking.
Scenario 4: Canonical Host Names
The final SEO problem I discussed earlier are scenarios
where a site works with both a leading “www” hostname prefix as well as just
the hostname itself. This causes search engines to treat the URLs as
different and split search rankling:
http://www.scottgu.com/albums.aspx
http://scottgu.com/albums.aspx
We can fix this by adding a new IIS Rewrite rule that
automatically redirects anyone who navigates to the first URL (that has a www
prefix) to instead go to the second URL. Like before, we will setup the
HTTP redirect to be a “permanent redirect” – which will indicate to search
engines that they should follow the redirect and use the new URL they are
redirected to as the identifier of the content they retrieve.
To create such a rule we’ll click the “Add Rule” link in the
URL Rewrite admin tool again. This will cause the “Add Rule” dialog to
appear again:
The URL Rewrite admin tool has a built-in “Canonical domain
name” rule template.
When we select it and click the “ok” button we’ll see the
following dialog which asks us if we want to create a redirect rule that
automatically redirects users to a primary host name URL:
Above I’m entering the primary URL address I want to expose
to the web: scottgu.com. When we click the “OK” button we’ll get a
pre-written rule that automatically performs a permanent redirect if the URL
has another leading domain name prefix.
This will save the following additional rule to our
web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Cannonical Hostname">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll"
trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^scottgu\.com$"
negate="true" />
</conditions>
<action type="Redirect" url="http://scottgu.com/{R:1}" />
</rule>
<rule name="Default Document" stopProcessing="true">
<match url="(.*?)/?Default\.aspx$" />
<action type="Redirect" url="{R:1}/" />
</rule>
<rule name="Lower Case URLs" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<conditions logicalGrouping="MatchAll"
trackAllCaptures="false">
<add input="{URL}" pattern="WebResource.axd"
negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{URL}}" />
</rule>
<rule name="Trailing Slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions logicalGrouping="MatchAll"
trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory"
negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile"
negate="true" />
<add input="{URL}" pattern="WebResource.axd"
negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>