Published on

Redirecting www.* subdomain to the root domain in IIS 7

Authors
  • avatar
    Name
    Jac Timms
    Twitter

Having your site available on www.yourdomain.com and yourdomain.com isn't good for your SEO as it splits the domain authority. You can use the Google Webmaster Tools to specify which domain Google should use, but you should also redirect on the site itself. You can do this in your web.config directly, but IIS has a module to do it for you. Redirecting from a sub-domain in IIS 7 is really easy using the URL Rewrite feature.

First select the URL Rewrite module for your domain:

URL Rewrite module in IIS 7

Click Add Rule in the top right panel and select Canonical domain name.

Add Rule dialog in IIS 7

Add the your domain name without the www.

Canonical domain name configuration

You will see your rule has been created…

Rule creation confirmation

Now we need to make the redirect send the 301 HTTP header, which makes it a permanent redirect, so select your rule and hit "Edit…" in the right side bar, scroll to the bottom and change the Redirect type to Permanent (301).

Permanent redirect configuration

This will make your web.config file something like this…

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="CanonicalHostNameRule1">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^scorch.io$" negate="true" />
          </conditions>
          <action type="Redirect" url="http://scorch.io/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>