Public Function seoPageUrls(ByVal thisProductId As Integer) As String '### CONNECT TO DATABASE - CHANGE yourDSN IN YOUR WEB.CONFIG FILE ACCORDINGLY Dim myConnection as New oleDBConnection(ConfigurationManager.AppSettings("yourDSN")) Dim myCommand as New oleDBCommand Dim myReader As OleDbDataReader Dim thisProductName As String = "" '### GET THE ACTUAL PRODUCT NAME FROM THE DATABASE myCommand.Connection = myConnection myCommand.CommandText = "SELECT productName FROM productTable WHERE productId = " & thisProductId Try myConnection.Open() thisProductName = myCommand.ExecuteScalar() Finally myConnection.Close() End Try myConnection.Dispose() '### GET RID OF NON-FRIENDLY URL CHARACTERS AND REPLACES SPACES WITH DASHES thisProductName = Regex.Replace(thisProductName, "[^\w\@-]", " ").ToLower() thisProductName = Regex.Replace(thisProductName.Trim(), "\s{1,}", "-") '### JUST RETURN THE SEO SEARCH ENGINE FRIENDLY URL Return "/shoppingcart/" & thisProductId & "/" & thisProductName End Function Sub Application_BeginRequest(ByVal sender as Object, ByVal e as EventArgs) Try Dim httpContext As System.Web.HttpContext = httpContext.Current() Dim currentURL As String = Request.ServerVariables("PATH_INFO") httpContext.RewritePath(rewriteUrl(currentURL)) Catch ex As Exception Response.Write("Error in Global.asax:" & ex.Message) End Try End Sub Public Function rewriteUrl(byVal thisURL As String) Dim currentFileName As String Dim currentFileType As String Dim currentFileID As String Dim returnURL As String '### THIS REGEX STRING IS LOOKING FOR A URL IN THE FORM: /shoppingcart/4757/name-of-my-product.aspx '### SO THE URL WILL GET READ LIKE THIS: '### fileType = shoppingcart '### identifier = 4757 '### fileName = name-of-my-product.aspx Dim rewrite_regex As Regex = New Regex("^.*/(?[^/]+)/(?\d+)/(?[^/]+)(\?.*)?", RegexOptions.IgnoreCase) Dim match_rewrite As Match = rewrite_regex.Match(thisURL) currentFileType = match_rewrite.Groups("fileType").Value.toString() currentFileID = match_rewrite.Groups("identifier").Value currentFileName = match_rewrite.Groups("fileName").Value.toString() If currentFileID <> "" Then If currentFileType = "shoppingcart" Then returnURL = "/shoppingcart.aspx?id=" & currentFileID Else '### NO MATCH, NO URL REWRITING - JUST PASS TO REQUESTED URL returnURL = thisURL End If Else returnURL = thisURL End If Return returnURL End Function