Wednesday, December 12, 2012

Redirect www subdomain to root domain in ASP.NET

Let's say we want to perform permanent redirect from http://www.example.com address to http://example.com in our ASP.NET application for SEO purposes(URL canonization).

First of all, we need to download Rewrite module for IIS7:
http://www.microsoft.com/web/gallery/install.aspx?appid=urlrewrite2

Then you need to open IIS Manager and navigate to URL Rewrite section.

After proper configuration in IIS Manager you'll get section in your Web.config that can look like:
 <configuration>  
   <system.webServer>  
     <rewrite>  
       <rules>  
         <rule name ="CanonicalHostNameRule" stopProcessing="true">  
           <match url="(.*)" />  
           <conditions>  
             <add input ="{HTTP_HOST}" pattern="^www\." negate="false" />  
           </conditions>  
           <action type ="Redirect" url="http://example.com/{R:1}" />  
         </rule>  
       </rules>  
     </rewrite>  
   </system.webServer>   
 </configuration>  

No comments:

Post a Comment