Hi people, Im trying to implement url rewriting in my app to make it more spider friendly. Im using the following code, which I got from searching the web /App_Code/RegExUrlMappingBaseModule.vb Imports Microsoft.VisualBasic Imports System.Web Namespace RegExUrlMapping_HTTPModule Public Class RegExUrlMappingBaseModule Implements System.Web.IHttpModule Sub Init(ByVal app As HttpApplication) Implements IHttpModule.Init AddHandler app.AuthorizeRequest, AddressOf Me.BaseModuleRewriter_AuthorizeRequest End Sub Sub Dispose() Implements System.Web.IHttpModule.Dispose End Sub Sub BaseModuleRewriter_AuthorizeRequest(ByVal sender As Object, ByVal e As EventArgs) Dim app As HttpApplication = CType(sender, HttpApplication) Rewrite(app.Request.Path, app) End Sub Overridable Sub Rewrite(ByVal requestedPath As String, ByVal app As HttpApplication) End Sub End Class End Namespace /App_Code/RegExUrlMappingConfigHandler.vb Imports Microsoft.VisualBasic Imports System.Configuration Imports System.Xml Namespace RegExUrlMapping_HTTPModule Public Class RegExUrlMappingConfigHandler Implements IConfigurationSectionHandler Dim _Section As XmlNode Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object Implements System.Configuration.IConfigurationSectionHandler.Create _Section = section Return Me End Function ''' Get whether url mapping is enabled in the app.config Friend Function Enabled() As Boolean If _Section.Attributes("enabled").Value.ToLower = "true" Then Return True Else Return False End If End Function ''' Get the matching "mapped Url" from the web.config file if there is one. Friend Function MappedUrl(ByVal url As String) As String Dim x As XmlNode Dim oReg As Regex For Each x In _Section.ChildNodes oReg = New Regex(x.Attributes("url").Value.ToLower) If oReg.Match(url).Success Then Return oReg.Replace(url, x.Attributes("mappedUrl").Value.ToLower) End If Next Return "" End Function End Class End Namespace /App_Code/RegExUrlMappingModule.vb Imports Microsoft.VisualBasic Imports System.Web Imports System.Configuration Namespace RegExUrlMapping_HTTPModule Public Class RegExUrlMappingModule Inherits RegExUrlMappingBaseModule Overrides Sub Rewrite(ByVal requestedPath As String, ByVal app As HttpApplication) ''Implement functionality here that mimics the 'URL Mapping' features of ASP.NET 2.0 Dim config As RegExUrlMappingConfigHandler = CType(ConfigurationManager.GetSection("system.web/RegExUrlMapping"), RegExUrlMappingConfigHandler) Dim pathOld As String, pathNew As String = "" If config.Enabled Then pathOld = app.Request.RawUrl ''Get the request page without the querystring parameters Dim requestedPage As String = app.Request.RawUrl.ToLower If requestedPage.IndexOf("?") > -1 Then requestedPage = requestedPage.Substring(0, requestedPage.IndexOf("?")) End If ''Format the requested page (url) to have a ~ instead of the virtual path of the app Dim appVirtualPath As String = app.Request.ApplicationPath If requestedPage.Length >= appVirtualPath.Length Then If requestedPage.Substring(0, appVirtualPath.Length).ToLower = appVirtualPath.ToLower Then requestedPage = requestedPage.Substring(appVirtualPath.Length) If requestedPage.Substring(0, 1) = "/" Then requestedPage = "~" & requestedPage Else requestedPage = "~/" & requestedPage End If End If End If ''Get the new path to rewrite the url to if it meets one ''of the defined virtual urls. pathNew = config.MappedUrl(requestedPage) ''If the requested url matches one of the virtual one ''the lets go and rewrite it. If pathNew.Length > 0 Then If pathNew.IndexOf("?") > -1 Then ''The matched page has a querystring defined If pathOld.IndexOf("?") > -1 Then pathNew += "&" & Right(pathOld, pathOld.Length - pathOld.IndexOf("?") - 1) End If Else ''The matched page doesn't have a querystring defined If pathOld.IndexOf("?") > -1 Then pathNew += Right(pathOld, pathOld.Length - pathOld.IndexOf("?")) End If End If ''Rewrite to the new url HttpContext.Current.RewritePath(pathNew) End If End If End Sub End Class End Namespace /Web.Config <configuration> <!-- Declare the custom 'RegExUrlMapping' section and handler --> <configSections> <sectionGroup name="system.web"> <section name="RegExUrlMapping" type="RegExUrlMapping_HTTPModule.RegExUrlMappingConfigHandler"/> </sectionGroup> </configSections> <system.web> <!-- Tell ASP.NET to use the RegEx URL Mapping HTTP Module --> <httpModules> <add type="RegExUrlMapping_HTTPModule.RegExUrlMappingModule" name="RegExUrlMappingModule"/> </httpModules> <!-- The RegEx URL Mapping parser goes through these in sequential order. --> <RegExUrlMapping enabled="true"> <add url="~/DisplayUser.aspx" mappedUrl="~/DisplayUser.aspx?UserName=Helen"/> </RegExUrlMapping> </system.web> </configuration> Code (markup): The only change Ive made is to define the url i want mapped. When I log into my app and do a search of members and select my profile (Helen) then I want the url to read ...DisplayUser.aspx but even though the code executes with no errors, the ="~/DisplayUser.aspx?UserName=Helen url still displays. Can anyone suggest why this happens? Its been days now and I cant get my head around it.
Where did you "get" this source? Maybe if you tell us it's origin - we can look at its documentation (if it exists) and help walk you through it better?
Hi ccoonen, Thanks for the reply. I got it from this link http://pietschsoft.com/blog/post.aspx?postid=762 Its still causing me major problems. Ive tried a number of other pieces of code Ive found too but I think the underlying problem is that I cant get my head around the whole principle. Ive considered using Sessions instead, so when I click on the members grid it writes the member name into a session variable and picks it up in the DisplayUser.aspx PageLoad. I think this makes the app spider friendly - are there any major problems associated with this approach? I know its a bit lazy but if I dont figure out the url rewriting soon I think Ill have to go with it