This blog post is more of a future reference for myself. Anytime I create a new site I use it, but I can never remember the syntax.
To search engines, it's no good if your homepage can be shown under several URLs. Consider the 4 different ways someone could view the rshelby.com homepage:
All 4 examples listed above lead to the same page. However since they each have a different URL, search engines may view them as several pages with the same content. It is known that Google has penalties for duplicate content.
Another issue is when people link to your site. Quite often they copy and past the URL of your homepage from the address bar. As a result your inbound links might not all be the same, and you may not get the full benefit of the links going to your homepage.
A perfect example of this is www.vectorlight.net. www.vectorlight.net currently has a Google Page Rank of 4, while www.vectorlight.net/default.aspx has a rank of 2. It's the exact same page. Could Vectorlight really have a page rank of 5 if all roads lead to www.vectorlight.net?
To eliminate this potential problem, I use the IIS 7 Rewrite Module to force all 4 scenarios to use the one URL of my choice [that would be www.rshelby.com/]. IIS 7 Rewrite Module must be installed on your webserver in order to use it. It can be downloaded at IIS 7 Rewrite Module.
The nice thing about it is you can define everything in the System.webServer namespace within your Web.Config file. This makes life very easy in a shared hosting environment if you don't have access to the IIS 7 Rewrite Module user interface.
To force a 301 redirect from non-www to www:
1 <system.webServer>
2 <rewrite>
3 <rules>
4 <rule name="Redirect to WWW" stopProcessing="true">
5 <match url=".*"/>
6 <conditions>
7 <add input="{HTTP_HOST}" pattern="^yourwebsite.com$"/>
8 </conditions>
9 <action type="Redirect" url="http://www.yourwebsite.com/{R:0}" redirectType="Permanent"/>
10 </rule>
11 </rules>
12 </rewrite>
13 </system.webServer>
To force a 301 redirect from www to non-www, change the rule to.
1 <rule name="Redirect to NON-WWW" stopProcessing="true">
2 <match url=".*" />
3 <conditions>
4 <add input="{HTTP_HOST}" pattern="^www.yourwebsite.com$" />
5 </conditions>
6 <action type="Redirect" url="http://yourwebsite.com/{R:0}" redirectType="Permanent" />
7 </rule>
To force a 301 redirect from the Default.aspx to extensionless URL "/":
1 <rule name="Default Document" stopProcessing="true">
2 <match url="(.*)default.aspx"/>
3 <action type="Redirect" url="{R:1}" redirectType="Permanent"/>
4 </rule>
Since I'm using BlogEngine.NET software, it's much easier to use the builtin feature that can handle the www to non-www (or vice versa) redirect issue more easily. If you apply the Default Document rule in BlogEngine.NET, it won't let you sign in or out from the homepage.
At any rate, I was able to apply these rules pretty easily to my wife's salon website and it worked great. Try clicking on the following links and you'll see the IIS 7 Rewrite Module in action:
Even though the URL being requested is not the same, it still rewrites it as [www.hairsalonmichigan.com/]!