Redirect (301 ) Non WWW to WWW domain using Web.config in ASP.NET

Whenever we host a domain on server, we are able to access the webpage from as well as 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 to, edit your web.config file and add the following segment under the <system.webserver> section. Ensure you modify the words “” with your appropriate domain name.

  1. <rewrite>  
  2.     <rules>  
  3.         <rule name="Redirect to HTTP" patternSyntax="ECMAScript" stopProcessing="true">  
  4.             <match url=".*"></match>  
  5.             <conditions>  
  6.                 <add input="{HTTP_HOST}" pattern="^$"></add>  
  7.                 <add input="{HTTPS}" pattern="off"></add>  
  8.             </conditions>  
  9.             <action type="Redirect" url="{R:0}" redirectType="Permanent" appendQueryString="true"></action>  
  10.         </rule>  
  11.     </rules>  
  12. </rewrite>   
Hope you like this. Keep learning and sharing.