You are here

IIS7 - Www Redirect

wwwWhen setting up a website with your own domain name, you always have a choice of which name to use - with www prefix or without.

You can point both - the domain and www to the same website and forget about it, letting the search engines and people use whatever variant they want, but I personally prefer to stick to just one variant and have it enforced, so that all clients would always see the same Url. This sounds as a good idea as you would not need to worry about things like cookies, seo, cross domain references, etc.

But how to implement it?

Some hosting companies, like Godaddy, would let you enable domain forwarding and you can play with it to achieve the desired behavior. If you do not have such an ability and you are running windows wih IIS7 and Url Rewrite module installed, you can try the following rule:


IIS7 Www Redirect

IIS Url Rewrite rule configured as on the picture, will issue a 301 redirect when request comes without the www prefix, preserving the url path and query string, so you can use this solution even if your website is known under both names at the moment.

If you do not have access to the management screens, but have access to the web.config, you can use the following xml rule:

                <rule name="Domain to www redirect" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTP_HOST}" pattern="^klotin\.com$" />
                    <action type="Redirect" url="{HTTP_URL}" />

You can also use such rules for the domain aliasing or for the website domain name change. The only thing is that the rule will look differently if you want to support both - SSL and regular connections.

Another probably slightly easier approach to do the same is by using 'Canonical domain name' rule template:

Canonical domain name template IIS7

When using this template, you just need to pick the primary host name you would want to use from all of those, defined on the web site bindings screen.

Social Buttons: 

Add new comment

Get Updates

RSS Twitter

Subscribe by Email:

Recent Comments

Who's online

There are currently 0 users online.