Introduction:
In this article we will explain how to redirect non www to www by using IIS 7 and IIS 8 with example.
Description:
The 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.
Let’s get started. First, open IIS Manager and select your website and double-click on the “URL Rewrite” icon.
Next, click on “Add Rules…” from the Actions pane.
Now select blank rule from wizard options and click ok button.
Now Give your rule “Name”. I’ll call mine “non www to www”.
In the “Using” dropdown box you can choose between Matches the Pattern and Wildcards.
Enter * for the “Pattern”. That means anything qualifies. We’ll use a condition later instead of matching to the URL. (for Matches the Pattern, use .*).
Now expand the “Conditions” section and click “Add.
”. In the “Add Condition” dialogue enter the following details and 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/contactus it would retain the contactus as it adds the www.
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.
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>
This is just the start to great SEO, but it’s a common step and one that I hope you find helpful.
Leave Comment