August 17, 2015 by Nitesh

301 Redirect Non WWW to WWW Using Web.config in ASP.Net

Friends,

Whenever we host a domain on server, we are able to access the webpage from http://www.mydomain.com as well as http://domain.com. For SEO purposes, this is not good as Google and other search engines penalises the website as duplicate content. To resolve this, we can redirect the non-www domain to www domain.  In this post, we will see how can we do a “301 Redirect of a non WWW domain to WWW domain using web.config file in ASP.Net”.

To redirect mydomain.com to www.mydomain.com, edit your web.config file and add the below segment under the <system.webserver> section. Ensure you modify the words “mydomain.com” with your appropriate domain name.

<rewrite>
      <rules>
            <rule name="Redirect http://mydomain.com to http://www.mydomain.com HTTP" patternSyntax="ECMAScript" stopProcessing="true">
          <match url=".*"></match>
          <conditions>
            <add input="{HTTP_HOST}" pattern="^mydomain.com$"></add>
            <add input="{HTTPS}" pattern="off"></add>
          </conditions>
          <action type="Redirect" url="http://www.mydomain.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
        </rule>
      </rules>
    </rewrite>

Hope you like this. Keep learning and sharing. 🙂

#ASP.Net#IIS#web.config
  • Manoj Kalla

    Good article.. its save my time.. Please keep it up good work.

  • Mr.R

    thanks thankssss <3

  • Boomlike

    Great job.

  • Martin Lukes

    Super… And… How to redirect the non-www to www but also HTTP to HTTPS? Please

Support us!

If you like this site please help and make click on any of these buttons!

Powered by WordPress Popup