Search code examples
asp.netvb.nethttp-status-code-301url-rewriting

URL Rewriting and 301 Redirect... Redirects to the original URL


In answering another persons question here on SO, I discovered that there is a small "bug" in my global redirect code.

I have wired up a Global class to an HttpModule. It's job is to detect "http:/www." in the URL and redirect the user to the NON www. version

Protected Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    'Force Removal of WWW
    Dim application As HttpApplication = TryCast(sender, HttpApplication)
    Dim url As Uri = application.Context.Request.Url
    Dim hasWWW As Boolean = If(url.ToString.StartsWith("http://www."), True, False) 'UrlRegex.IsMatch(url.ToString())
    If hasWWW Then
        Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme))
        application.Context.Response.Redirect(newUrl, False)
        application.Context.Response.StatusCode = 301
        application.Context.Response.End()

    End If

End Sub

The problem I'm having is that when it redirect a page http://www.example.com/AboutUs, the goal is to have it go to http://example.com/AboutUs (the rewritten page) but instead it's going to http://example.com/Default.aspx?Slug=AboutUs (the original page).

I tried doing a bit of a hack by changing

    Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme))
    application.Context.Response.Status = "301 Moved Permanently"
    application.Context.Response.AddHeader("Location", newUrl.Replace("Default.aspx", "")) 

to

    Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme))
    newUrl = newUrl.Replace("Default.aspx?Slug=", "")
    newUrl = newUrl.Replace("Default.aspx", "")
    application.Context.Response.Status = "301 Moved Permanently"
    application.Context.Response.AddHeader("Location", newUrl) 

not something I want to do anyways since it's a hack, but it didn't work anyways.

Any advice on this would be very much appreciated!


Solution

  • Here's the answer that works

        Protected Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
            'Force Removal of WWW
            Dim application As HttpApplication = TryCast(sender, HttpApplication)
            Dim url As Uri = application.Context.Request.Url
            Dim hasWWW As Boolean = If(url.ToString.StartsWith(String.Format("{0}://www.", url.Scheme)), True, False)
            Dim forceWWW As Boolean = Boolean.TryParse(ICMS.Site.Settings.GetSettingsValue("ForceWWW"), False)
            'UrlRegex.IsMatch(url.ToString())
            If hasWWW Then
                Dim newUrl As String = UrlRegex.Replace(url.ToString(), String.Format("{0}://", url.Scheme))
                application.Context.Response.Redirect(newUrl.Replace("Default.aspx?Slug=", String.Empty), False)
                application.Context.Response.StatusCode = 301
                application.Context.Response.End()
            End If
    
        End Sub