IIS URL Rewrite – rewriting non-www to www
If you’re using IIS 7.0 (or 7.5), URL Rewrite is a valuable tool, well worth installing and using.
One common use of URL Rewrite is redirecting http://domain.com to http://www.domain.com. Many people are doing this for search engine optimization (SEO) so that search engines only see the one site, rather than two sites. The goal is to set a permanent 301 redirect.
You can download URL Rewrite from http://www.iis.net/expand/URLRewrite. For this walkthrough and screenshots I’ll use URL Rewrite 2.0 RC1, but everything that I’ll cover also works for version 1.0 and 1.1.
URL Rewrite works at the global level, or site level (or application level for that matter). Where you apply it is really up to how you manage your server. Either will work for a domain name redirect like this.
You can choose to create the rules using IIS Manager, or using a text editor and updating web.config directly. I’ll show both, starting with IIS Manager.
Let’s get started. First, open IIS Manager and double-click on the “URL Rewrite” icon.
Next, click on “Add Rules…” from the Actions pane.
Here you’ll have a choice from a few wizard options, and with URL Rewrite 2.0 you can also create outbound rules. Create a Blank rule (inbound rules).
Give your rule a good friendly “Name”. I’ll call mine “Redirect domain.com to www”.
In the “Using” dropdown box you can choose between Regular Expressions and Wildcards. Use wildcards if you aren’t familiar with regular expressions since they are much more intuitive. However, if you later need to create more complex rules, regex may be necessary.
For this demo select Wildcards. However, I’ll include instructions for those wanting to use regular expressions.
Enter * for the “Pattern”. That means anything qualifies. We’ll use a condition later instead of matching to the URL. (for Regular Expressions, use .*).
Now expand the “Conditions” section and click “Add”. In the “Add Condition” dialogue enter the following:
Condition input: | {HTTP_HOST} |
Check if input string: | Matches the Pattern |
Pattern: | domain.com (for regex, enter ^domain.com$) |
Ignore case: | checked |
Click OK.
Finally, it’s time to set the Action.
In the Action section make sure that the “Action Type” is set to Redirect.
For the “Action Properties”, enter http://www.domain.com/{R:0}. The {R:0} retains the existing URL so if someone typed something like http://domain.com/aboutus it would retain the aboutus as it adds the www.
Note (added later): If you want this to be more generic to account for multiple domain names and to retain the original domain name in the redirect, check out this blog post.
Be sure that the “Append query string” remains checked so that the querystring part is also retained.
Also, be sure that the “Redirect Type” is set to Permanent (301), which is what the search engines like. This tells the search engines to do a permanent redirect, use the new location and ignore the previous location.
Finally, Apply the rule and test!
Using a Text Editor
You can also create this rule manually by adding the following to your site’s web.config (or applicationHost.config if you set this at the server level).
In the <system.webServer> section of your web.config, add the following:
Wildcards
<rewrite>
<rules>
<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Save and you should be set.
Or, if you prefer Regular Expressions, use this instead:
Regular Expressions
<rewrite>
<rules>
<rule name="Redirect domain.com to www" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
This is just the start to great SEO, but it’s a common step and one that I hope you find helpful.
See Part II on how to redirect multiple domain names to a single domain name: http://weblogs.asp.net/owscott/archive/2009/11/30/iis-url-rewrite-redirect-multiple-domain-names-to-one.aspx
Also see this blog post on how to do a generic redirect while retaining the original domain name.