Archive for the ‘IIS’ Category

Redirect non www to www domain

Posted: August 22, 2011 in ASP.NET, IIS, SEO

Some time we need to force the user to visit out website with www prefix like http://www.somedomain.com instead of http://somedomain.com. There are many valid reasons to do this, one important reason is to prevent the Search Engines from treating our website as two different websites, one with www and one without www.

It is very much easy to accomplish this, just copy paste the following code inside the system.webserver node in your web.config.

 <rewrite>
            <rules>
                <rule name="Redirect pakpassport.com to www" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^somdomain.com$" />
                    </conditions>
                    <action type="Redirect" url="http://www.somedomain.com/{R:0}" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
Replace the somedomain.com with your domain information.
{R:0} indicates the other things in the requested url.
and RedirectType="Permanent" indicates look this site in this way in the future i.e. with www prefix.