Search code examples
asp.netvb.netglobal-asax

ASP.NET site for redirecting many URLs fails 404 when redirecting URLs with stems


.net 4.8, VS 2019, VB.NET, IIS 10 running on Windows 2016 VM in Azure.

I've recently rearchitected my websites. I have many domain names, and can't get past this issue.

I set up a table in SQL Server. Example data:

SiteRedirectorID ServerName UrlForNoStem UrlForPreservedStem
1 arf.com [h t t p s : / / (example dot com)/arf.html] [h t t p s : / / (example dot com)/]
2 ribbit.com [h t t p s : / / (example dot com)/frognoise.html] [h t t p s : / / (example dot com)/]
3 meow.com [h t t p s : / / (example dot com)/meow.html] [h t t p s : / / (example dot com)/]
4 trumpet.com [h t t p s : / / (example dot com)/elephants] [h t t p s : / / (example dot com)/]
5 www.bowwow.com [h t t p s : / / (example dot com)/] [h t t p s : / / (example dot com)/]

All of the server names are bound to the app here, but the server(s) in the Url named columns are on different apps, perhaps different machines.

The redirector app has no code except in global.asax, and here is all of that code. Default.aspx exists but is blank and no code-behind.

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim uCurrent As Uri = HttpContext.Current.Request.Url
    Dim sReqHost As String = uCurrent.Host
    Dim sReqUrl As String = uCurrent.AbsolutePath.Trim()
    Dim sFullTail As String = uCurrent.PathAndQuery.Trim()
    Dim sSearchHost As String = sReqHost.ToLower()
    If sSearchHost.StartsWith("www.") Then
        sSearchHost = sSearchHost.Replace("www.", "")
    End If
    If sSearchHost.StartsWith("ww.") Then
        sSearchHost = sSearchHost.Replace("ww.", "")
    End If
    If sSearchHost.StartsWith("w.") Then
        sSearchHost = sSearchHost.Replace("w.", "")
    End If
    Dim sConn As String = ConfigurationManager.ConnectionStrings("WS").ToString
    Using DS As New DataSet()
        Using Conn As New SqlClient.SqlConnection(sConn)
            Conn.Open()
            Dim sSQL As String = "select * from [SiteRedirector] where [ServerName] = @ServerName; "
            Using Cmd As New SqlClient.SqlCommand(sSQL, Conn)
                Cmd.Parameters.Add("@ServerName", SqlDbType.VarChar).Value = sSearchHost
                Using DA As New SqlClient.SqlDataAdapter(Cmd)
                    Dim iRet As Integer = DA.Fill(DS)
                End Using
            End Using
            Conn.Close()
        End Using
        If DS.Tables.Count < 1 OrElse DS.Tables(0).Rows.Count < 1 Then ' dead link
            Response.Redirect("https://example.com/404.aspx")
        End If
        Dim sTarget As String = ""
        If sFullTail.Trim() = "/" OrElse sFullTail.Trim() = "" Then
            sTarget = DS.Tables(0).Rows(0)("URLForNoStem").ToString.Trim()
        Else
            sTarget = DS.Tables(0).Rows(0)("URLForPreservedStem").ToString.Trim()
            If sTarget.EndsWith("/") AndAlso sFullTail.StartsWith("/") Then
                sTarget &= sFullTail.Trim().Substring(1, sFullTail.Length - 1)
            Else
                sTarget &= sFullTail.Trim()
            End If
        End If
        Server.ClearError()
        Response.Clear()
        Response.Redirect(sTarget)
    End Using
End Sub

When testing locally and using the immediate window to inject test URLs into the uCurrent variable, everything works perfectly for all URL and stem combinations. Exactly as designed.

However, when deployed to IIS, the URLs with no stem [h t t p : / / ribbit.com] works but something with a stem: [h t t p : / / meow.com/hiss-scratch.html] or [h t t p : / / ribbit.com/backbeat.html?FBCLID=whatever] fails with a 404. The 301 redirect seems to be overridden after issuance by my redirector site's processing.

What am I missing?


Solution

  • The answer was based on the fact that .html extensions are not normally routed to global.asax. I was able to use the old Intellegencia URLRewriter and get the program to intercept the 404 and send it to the Application_Error event.