locked
Rewriting URLs based on a value from a database RRS feed

  • Question

  • User-1848872465 posted
    I want to rewrite URLs from this: http://www.mydomain.com/identifier/path/page.aspx?param=value To this: http://www.mydomain.com/path/page.aspx?id=identifier¶m=value The problem is, the "identifier" part needs to be matched against a database table, so it could be anything (ie I can't match it with a regex). I thought about adding a "go" pseudo-folder in front of the identifier, eg: http://www.mydomain.com/go/identifier/path/page.aspx?param=value Which would let me match the "/go/" - but I'd rather avoid this if possible. Is there a way? Mike
    Tuesday, June 1, 2004 2:29 PM

All replies

  • User-1645088262 posted
    Hi Jizepi We have a database lookup feature in our ASPRedirector.NET product. This will let you use values from a database in the replacement URL with the lookup based on some match in the original URL if required. See: ASPRedirector.NET Let me know if you need any help with the configuration.
    Tuesday, June 8, 2004 6:44 PM
  • User2101614113 posted
    Here's a fairly complete solution, thanks to the fact that I just coded these routines for myself yesterday. My "stripFrom" routines just parse the query string mechanically. The mikeIdentifierParser I slapped together to parse out your identifer. It works on both: http://www.mydomain.com/identifier/path/page.aspx?param=value and 404;http://localhost:80/myApp/identifier/path/page.aspx?param=value The two routines I've included that aren't called by mikeIdentiferParser will be useful for you to finish parsing out the rest of the rightOfIdentifer portion in order to construct your rewritten URL as you want. I hope this helps. Private Structure ParsedValues Dim mikeIdentifer As String Dim queryStringRightOfIdentifer As String End Structure Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) Dim queryString As String = Request.ServerVariables("QUERY_STRING").ToString Dim mikeVals As ParsedValues mikeVals = Me.mikeIdentifierParser(queryString) Dim identifer as String = mikeVals.mikeIdentifer Dim rightOfIdentifier as String = mikeVals.queryStringRightOfIdentifer 'Continue parsing here 'Call RewritePath to rewrite your path End Sub Private Function mikeIdentifierParser(ByVal queryString As String) As ParsedValues Dim returnVal As ParsedValues queryString = System.Web.HttpUtility.UrlDecode(queryString) 'strip http:// or https:// or 404;http:// etc. Me.stripFromLeftToDelimiterIfAny(queryString, "http://") Me.stripFromLeftToDelimiterIfAny(queryString, "https://") 'strip localhost if any If queryString.Length >= 9 Then If queryString.Substring(0, 9).ToLower = "localhost" Then Me.stripFromLeftToDelimiterIfAny(queryString, "/") End If End If 'strip domain (or localhost directory) Me.stripFromLeftToDelimiterIfAny(queryString, "/") 'strip out Mike's identifer returnVal.mikeIdentifer = Me.stripFromLeftToDelimiterIfAny(queryString, "/") returnVal.queryStringRightOfIdentifer = queryString Return returnVal End Function Private Function stripFromLeftToDelimiterIfAny(ByRef theString As String, ByVal theDelimiter As String) As String 'Searches for theDelimiter from left to right, if found returns the string to the left of the delimiter and modifies theString cutting the delimiter and everything to the left. Dim strippedPartWithoutDelimiter As String = "" Dim delimiterIndex As Integer = theString.IndexOf(theDelimiter) If delimiterIndex > -1 Then If delimiterIndex < theString.Length - theDelimiter.Length Then strippedPartWithoutDelimiter = theString.Substring(0, delimiterIndex) theString = theString.Substring(delimiterIndex + theDelimiter.Length) Else theString = "" End If End If Return strippedPartWithoutDelimiter End Function Private Function stripFromRightToDelimiterIfAny(ByRef theString As String, ByVal theDelimiter As String) As String 'Searches for theDelimiter from right to left, if found returns the string to the right of the delimiter and modifies theString cutting the delimiter and everything to the right. Dim strippedPartWithoutDelimiter As String = "" Dim delimiterIndex As Integer = indexOfLast(theString, theDelimiter) If delimiterIndex > -1 Then If delimiterIndex > 0 Then strippedPartWithoutDelimiter = theString.Substring(delimiterIndex + theDelimiter.Length) theString = theString.Substring(0, delimiterIndex) Else theString = "" End If End If Return strippedPartWithoutDelimiter End Function Private Function indexOfLast(ByRef theString As String, ByRef theSearchString As String) As Integer Dim i As Integer = theString.Length - theSearchString.Length Do While i > -1 If theString.Substring(i, theSearchString.Length) = theSearchString Then Return i End If i -= 1 Loop Return -1 End Function
    Saturday, June 19, 2004 7:06 PM