How to fix canonical default page with a 301 redirect on IIS.


A ThatsIT Solutions Tutorial

Search engines treat websites that can be accessed by more than one domain name, such as www.contoso.com and contoso.com, as if they are two different sites. This affects the page ranking for the web site. Use this rule template to create a redirect rule that will enforce the use of a single domain name for the website.

The default page where it by default.aspx, index.html, index.php or some other format should not be used in internal linking and if a request is made for this page the website should 301 redirect to the canonical domain name. When using Ajax or post-backs this can cause a problem and stop your Ajax or post-backs from working. This tutorial will show you have to do the redirects for default page and also how to make a simple fix to get around the Ajax the very frustrating post-backs problem.

Using IIS manager

The easiest way to fix canonical issues on Microsoft Windows Servers is using the url-rewire add-in for IIS. If you don’t have the URL rewrite tool you can download it using the Web Platform Installer. If you don't have access to the IIS Manager you can place the code at the bottom of this page directly in the web.config file.

  1. Open your IIS Manager and highlight the website you want to apply the rules to.
  2. Double click url-rewrite.
    Url rewrite
  3. Click add rules from the right menu.
  4. Select the blank inbound rule.
    SEO rule templates
  5. Give the rule a name.
  6. In pattern enter your regular expression, depending on your default page, here is the regular expression for default.aspx, ^default.aspx$.
  7. Make sure Ignore case is selected.
  8. For action type select redirect.
  9. For the Action properties section enter / as redirect URL.
  10. Select append query string as required.
  11. For redirect type select Permanent (301).
    Canonical default page rule

All done, now for the Ajax post back fix. Go back into our new rule; we need to add a condition.

Since post-backs have the request type of POST, we can trap only the request types of GET.

  1. For logical grouping we want Match All.
  2. Condition input should be {REQUEST_METHOD}.
  3. Set check if input string to equal Matches the pattern.
  4. For Pattern set it to GET.
  5. Select Ignore case and we are done.
    Request get method

That’s it all done. You can now look in your web.config and see the results.

In the web.config file

<system.webServer>
    ........
    <rewrite>
        <rules>
            <rule name="Default Page" enabled="true" stopProcessing="true">
                <match url="^default.aspx$" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_METHOD}" pattern="GET" />
                </conditions>
                <action type="Redirect" url="/" />
            </rule>
        </rules>
    </rewrite>
    ........
</system.webServer>