Last post Feb 15, 2010 09:29 AM by anzer
Feb 15, 2010 08:12 AM|TJNevis|LINK
I have been searching for a while and I've found that there are a few different ways of doing a URL rewrite. I'm not 100% sure I am finding exactly what I want, and from what code I've gotten to work and tested, it hasn't worked as planned.
I want files and folders to work as usual:
domain.com goes to domain.com/Default.aspx
domain.com/folder/file.aspx would go to that file
I am looking for any text after the slash, with no .aspx and before the next slash if applicable, to go to a page (that I don't have set up yet), which will act as a query string for the data controls I will have on that page.
Example, domain.com/username. I want it to actually go to domain.com/profiles.aspx?username, but display still, domain.com/username in the URL.
This is the code I have come across and put in my Global.asax. Right now it seems to be at an infinite loop, because every time the page loads, this matches and executes again. Probably the regex. Will this code do what I need it to do? or only partially
what I need it to do?
Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
Dim incoming As HttpContext = HttpContext.Current
Dim oldpath As String = incoming.Request.Path.ToLower()
Dim pageid As String
' page id requested
' Regular expressions to grab the page id from the pageX.aspx
Dim regex As New Regex("(\w+)/", RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace)
Dim matches As MatchCollection = regex.Matches(oldpath)
If (matches.Count > 0) Then
pageid = matches(0).Groups(1).ToString()
incoming.RewritePath("~/profile.aspx?" & pageid)
Feb 15, 2010 09:29 AM|anzer|LINK
To make the extension less urls work, you need a custom ISAPI url rewriter in your server.. http://www.isapirewrite.com/
Then you can configure your web.config to work the patterns the way you want..
If you are OK with urls with extensions (eg:- domain.com/username.aspx) , you can use custom URL rewriting dlls such as http://urlrewriter.net/, which doesn't need any configurations in the server... This one also work using the pattern matching you mentioned
in the web.config file..
Please visit this link for a detailed study on .net url rewriting options