views:

486

answers:

4

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!

A: 

Instead of this:

application.Context.Response.Status = "301 Moved Permanently"
application.Context.Response.AddHeader("Location", newUrl) 

Try this:

application.Context.Response.Redirect(newUrl, false)
application.Context.Response.StatusCode = 301
application.Context.Response.End()
Nick Craver
That produces the same behavior as my original code.
rockinthesixstring
@rockinthesixstring - You have a test page so I can see the headers it's sending?
Nick Craver
yup, http://staging.infinitas.ws:83/
rockinthesixstring
rockinthesixstring - Can you give me a page under there that *should* be redirecting me?
Nick Craver
every navigation link is rewritten. So if you click on "Contact Us", the link is http://staging.infinitas.ws:83/Contact-Us ... If you manually append the WWW. in front of it and then press [enter], you'll see the incorrect address (http://staging.infinitas.ws:83/Default.aspx?Slug=Contact-Us)
rockinthesixstring
@rockinthesixstring - Still can't test...since your global script checks that the url starts with www, I tried: http://www.infinitas.ws/Contact-Us, http://www.staging.infinitas.ws/Contact-Us, each on port 80 and 83...is the problem that since your redirect code is wrapped inside `If hasWWW Then`, it won't run on staging?
Nick Craver
you could be right.. let me see if I can change it.
rockinthesixstring
I can't seem to be able to access it remotely, I'll have to do it when I get back to the office.
rockinthesixstring
Ok you should be able to try now. The primary url is `http://www.cms.infinitas.ws`
rockinthesixstring
@rockinthesixstring - Can you post current code? The response doesn't make any sense, newUrl is coming out just as you say...is the last block in your question the current code?
Nick Craver
the current code is the first block exactly. The last block was simply something I "tried" that didn't work.
rockinthesixstring
A: 

Use IIS and setup a defferent website. Do a permanent redirect, preserving the url.

localman
A: 

Try use Request.RawUrl, e.g.

Dim newUrl As [String] = UrlRegex.Replace(Request.RawUrl.ToString(), [String].Format("{0}://", url.Scheme))

Request.RawUrl should be the original URL from the actual http request.

JonoW
+1  A: 

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
rockinthesixstring