Redirect non-WWW to WWW on IIS 6

Redirecting non-WWW traffic (e.g 'example.com') to WWW (e.g. 'www.example.com') using IIS 6 requires adding a ‘Site Binding’ and adding a ‘URL Rewrite’.

Why Redirect Traffic to a Single Domain?

Google clearly states in Preferred domain (www or non-www)

[W]e may treat the www and non-www versions of the domain as separate references to separate pages

In other words, if you’re serving up non-www and www versions of your site, then you may be damaging your PageRank. Google allows users to specify a ‘preferred domain’ for indexing with its Webmaster Tools; however, there are still other search engines on the World Wide Web and not all clients use Webmaster Tools. It's important that the redirect is a 301 (permanent) redirect.

Redirect non-WWW to WWW (or WWW to non-WWW) on IIS 6

Search for how to redirect non-www to WWW for Apache (.htaccess), and you’ll find pages of relevant hits. The same documentation for IIS is scant and misleading.
In my case, I have no use for ‘www’; however, everyone has been trained from the time they could click a mouse to type ‘www’ before every URL. I want 'www.example.com' to redirect to 'example.com'.

Add a Site Binding

  1. Under ‘Actions’ click on ‘Bindings…’ to open ‘Site Bindings’

Open site bindings

In this example, the only binding is for host name ‘example.com’. I need to add 'www.example.com', so IIS will respond to requests for the WWW version of my domain.

  1. Click on the ‘Add…’ button

Add a site binding

  1. Now add the secondary domain. In this case, it is ‘www.example.com’.
  • Type: http (default)
  • IP address: All Unassigned (default)
  • Port: 80 (default)
  • Host name: www.example.com

Specify site binding host name

Now, IIS will respond to requests for the secondary domain, 'www.example.com'.

Add a ‘URL Rewrite’

Next, IIS needs to redirect traffic from 'www.example.com' to 'example.com'.

  1. Open the 'URL Rewrite' feature in IIS.

Open the 'URL Rewrite' feature

  1. Under 'Actions' click on 'Add Rule(s)...' to add a rewrite rule.

Add a URL Rewrite rule

  1. Select the 'Canonical domain name' rule template and click the 'OK' button.

Use the 'Canonical domain name' rule template

  1. Select the primary host name, which is the domain to which the secondary domain will redirect.

In this example, the primary domain is 'example.com' because' www.example.com' will redirect to 'example.com'. Click the 'OK' button to create the rule.

Select the primary host name

The dialog box will close, leaving the 'URL Rewrite' feature open.

  1. Open the newly-created rule to reveal the settings, which were generated by the rule template.

Review inbound rule settings

Below is a summary of these setting:

  • Name: CanonicalHostNameRules1
  • Rule template: Canonical domain name
  • Match URL
    • Requested URL: Match the Pattern
    • Using: Regular Expressions
    • Pattern: (.*)
    • Ignore case: Yes
  • Conditions
    • Condition input: {HTTP_HOST}
    • Check if input string: Does Not Match the Pattern
    •  Pattern: ^example\.com$
    • Ignore case: yes
  • Action
    • Action type: Redirect
    • Action Properties
    • Redirect URL: http://www.example.com/(R:1)
    • Append query string: yes
    • Redirect type: 301

Now, when IIS handles a request for 'http://www.example.com/path/to/page' it is rewritten as 'http://example.com/path/to/page'.

Add new comment