How To Do 301 Redirect

Like you, one day i was also surfing to the internet to find out a way out redirecting my non www domain to www. I read many articles which give me a trick to do it using .htaccess file but as i was using a shared hosting with IIS server so it was not possible using the .htaccess file. luckily i stumbled to a post which give me a code that i need to put in my web.config file to redirect my non www to www domain. As per the instruction mentioned in the post i put the code in my web.config file but nothing happened. I start reading other posts on internet, all of the web guru’s were suggesting the same thing. Than i realized there is something wrong with my server. When i did a whois lookup using a website who.is i came to know that i was using IIS 6.

Limited period offer: Buy Godaddy’s Economic Hosting Plan with Free Domain for $1 per month. Click to active the offer. Final price will be shown into the cart.

301 redirectAfter finding out my IIS version, i started reading few more posts about how to do 301 redirect using web.config in IIS 6. After reading few posts i find out that you can’t redirect the non www domain to www using the code that i had in IIS 6. The redirection was only possible in IIS 7 with the code that i had.

I put up a request to my host to transfer my site to a IIS 7 server. My host did the job in less than 2 hours. I put the code and it worked. So here is the code that i used to do 301 redirect for my asp.net website.

Requirements For The Below Code to Work:

  • web.config file
  • IIS 7 Server
  • Windows 2008

<system.webserver>
<rewrite>
<rules>
<rule name=”Redirect to WWW” stopprocessing=”true”>
<match url=”.*”></match>
<conditions>
<add input=”{HTTP_HOST}” pattern=”^yourdomainname.com$”></add>
</conditions>
<action type=”Redirect” url=”http://www.yourdomainname.com/{R:0}” redirecttype=”Permanent”>
</action></rule>

<rule name=”Default Document” stopprocessing=”true”>
<match url=”(.*?)/?default\.aspx$”>
<action type=”Redirect” url=”{R:1}/”>
</action></match></rule>
</rules>
</rewrite>
</system.webserver>

Don’t forget to replace yourdomainname.com  with your real domain name.

Redirecting your non www domain to www or vice a versa is very important in SEO. It help you deal with canonical issue. Google and other search engine suggest to do this redirection. Hope our post must have helped you deal with you question “How To Do 301 Redirect Non WWW to WWW in Web.Config”. For other tips & tutorial don’t forget to subscribe to our blog.