I am not looking for help, just posting a solution I found today. Many ASP.Net developers have posted questions around the internet looking for a simple way to add a 301 Redirect when using an IIS server. People with servers that support .htaccess files don't have this problem and most responses involve making changes to the IIS server which does not work if you are using a shared hosting environment or they involve constructing HTTP Modules requiring quite a bit of code across multiple forms including the web.config folder. The following solution involves little code and only requires you to change the Global.asax file.
The problem arose from a hosting switch. As someone with less than a year of development experience I did not notice that while the old host automatically directed traffic to http://www.mysite.aspx and that the new one produced identical results for both http://www.mysite.aspx and http://mysite.aspx which makes search engines think there are two sites with duplicate content. After my new host would not create a redirect at my request I started looking around, finding several methods that did not work, and added the following to my Global.asax folder:
This example is in Visual Basic. If you use C# you can find a good converter at http://www.developerfusion.com/tools/convert/vb-to-csharp/
Protected Sub Application_BeginRequest(ByVal sender As [Object], ByVal e As EventArgs)
'Declare the server URL ex:www.mysite.com
Dim server As String = Request.ServerVariables("SERVER_NAME")
'Declare the form being accessed ex: Default.aspx
Dim url As String = Request.ServerVariables("URL")
' Declare the query string in the URL
Dim querystring As String = Request.ServerVariables("QUERY_STRING")
' Merge the server name with the form ex: www.mysite.com/Default.aspx
Dim fullurl As String = server & url
' Create a string for any URL with a querystring ex: ?&categoryid=1
Dim tail As String = "?" & querystring
' Declare string you want replaced ex: www.
Dim patternwww As String = "www."
' Declare what you want it replaced with
Dim patternclear As String = String.Empty
' Create an if statement for URL's containing the string you dont want and containg query strings.
If fullurl.Contains(patternwww) And querystring <> Nothing Then
' Replace www. with nothing
Dim wwwrpl As String = fullurl.Replace(patternwww, patternclear)
' Build a string for the final URL
Dim targeturl As String = "http://" & wwwrpl & tail
' Create the 301 Redirect
Response.Clear()
Response.Status = "301 Moved Permanently"
Response.AddHeader("Location", targeturl)
Response.[End]()
End If
' Create an if statement for URL's containing the string you don't and don't contain query strings
If fullurl.Contains(patternwww) And querystring = Nothing Then
' Replace www. with nothing
Dim wwwrpl As String = fullurl.Replace(patternwww, patternclear)
' Build a string for the final URL
Dim targeturl As String = "http://" & wwwrpl
' Create the 301 Redirect
Response.Clear()
Response.Status = "301 Moved Permanently"
Response.AddHeader("Location", targeturl)
Response.[End]()
End If
End Sub
Conclusion
After testing the site using Microsoft's Http Verifier at http://www.microsoft.com/search/Tools/default.aspx I now get
URL: http://www.mysite.com
HTTP status code: 301 Moved Permanently
HTTP conditional GET: not enabled for the date selected
HTTP compression: not enabled
HTTP headers:
Content-Length: 0
Cache-Control: private
Date: Sun, 13 Sep 2009 03:39:04 GMT
Location: http://mysite.com/default.aspx
Server: Microsoft-IIS/6.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
AND
URL: http://mysite.com
HTTP status code: 200 OK
HTTP conditional GET: not enabled for the date selected
HTTP compression: not enabled
HTTP headers:
Content-Length: 43572
Cache-Control: private
Content-Type: text/html; charset=utf-8
Date: Sun, 13 Sep 2009 03:41:01 GMT
Server: Microsoft-IIS/6.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
I hope this works as good for you as it has for me and continues to work as good for as it appears to be working