Convert www. to non www. urls for ASP.Net websites June 16
Ok, redirecting your www. urls to non www. through IIS is a total pain. Using a url rewriter is a good option - but not always available in shared hosting. Here is a quick method if you have a small ASP.Net site or are using master pages.
Just add this code to the Page_Load sub in the master page, or to every page if you aren’t using master pages.
If Not IsPostBack Then
'redirect www. to non www.
If LCase(Request.Url.AbsoluteUri).StartsWith("http://www.") = True Then
Response.Status = "301 Moved Permanently"
Dim sNewLocation As String
sNewLocation = "http://" & Mid$(Request.Url.AbsoluteUri, 12)
If LCase(sNewLocation).EndsWith("/default.aspx") Then
sNewLocation = Left$(sNewLocation, Len(sNewLocation) - 13)
End If
Response.AddHeader("Location", sNewLocation)
Exit Sub
End If
End If
the code removes the www. and does a simple 301 redirect, while preserving case etc in the new url.
Come to think of it, now that Google can follow javascript redirects, you could add this code as a javascript sub to your pages. Anyone want to do a live test on their site?
