Hi,
Many sites will be having same HTML content or files available via different URLs. This creates content duplication also in some cases the non www URL must be redirected to www URL. Below are some cases:
http://www.example.com/
http://www.example.com/index.asp
http://www.example.com/default.asp
http://example.com/
http://example.com/index.asp
http://www.example.com/default.asp
In these cases we need to do canonical redirection, the code for canonical redirection in asp site is written in web.config & the code is as follows.
web.config 301 redirect
A 301 redirect allows visitors and search engines to access a web page after it has been moved. 301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. Here are instructions on how to code a 301 redirect.
The first example will redirect single pages to a new location. For example, http://domain.com/test.htm will change to http://domain.com/test.
1. Open web.config in the directory where the old pages reside
2. Then add code for the old location path and new destination as follows:
<configuration>
<location path="test.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://domain.com/test" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
You may add as many location paths as necessary.
Canonical Issues - Redirecting non-www to www:
Some times you prefer to redirect domain.com to http://www.domain.com for search engine optimization purposes. If your website is hosted by IIS7 and Microsoft's URL Rewrite Module is installed, then you can add rewriting rules in your Web.config file. Then the following code is used in the web.config file:
<configuration>
...
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yoursite\.com$" />
</conditions>
<action type="Redirect" url="http://www.yoursite.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
web.config Index Redirect
It redirects the index file to the root. The rule can be added to the web.config file and works with sites hosted on our Windows servers.
For example:
http://www.domain.com/index.asp
Is automatically redirected to:
http://www.domain.com
For that add the following code to your existing web.config file.
<rule name="redirect index.asp" stopProcessing="true">
<match url="^(\w*/)?index\.asp" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain\.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>