I provided more detailed installation instructions for Intelligencia's UrlRewriter.dll in my previous post URL Rewriting using Intelligencia UrlRewriter Example 1.This post will concentrate on a different implementation. This implementation consisted of a website where each member of the website would have their own URL to a web page containing web parts that were customizable by each member. The previous example consisted by a website with store categories and products. All links on the pages were generated by the system and therefore always had a full URL out to the page name, such as default.aspx. These well-formed page requests were easily handled by the URL Rewriter. In this example, a well-formed URL would be http://www.samplesite.com/JoeExample/default.aspx. Unfortunately, we can't always control how members would type in their own URL. This system has had to be prepared to handle the cases where a member would type in http://www.samplesite.com/JoeExample/ or http://www.samplesite.com/JoeExample (both are missing the page name default.aspx). The well-formed URL is handled by the URL Rewriter by putting the following code in the web.config file.
<rewriter>
<unless url="~/admin/(.+)">
<unless url="~/member/(.+)">
<rewrite url="~/(.+)/Default.aspx" to="~/member/Default.aspx" processing="stop" />
</unless>
</unless>
</rewriter>
I added the <unless> tags to skip any request going to my admin subdirectory and to skip any request that was being directed to the member subdirectory. If a request for "/JoeExmple/Default.aspx" came in, the system would re-direct them to the member directory. Note that I didn't have to add any querystring parameters to the re-written URL. This system uses ASPNet Authentication so by the time the visitor arrived in the member directory, they were already logged in and the system knew who they were.
I did not want to create empty stub directories for every member. This causes a problem if the visitor types in just /JoeExample or /JoeExample/ without any page name on the end. The directory does not exist so the system was going to my "Page Not Found" web page. To handle cases like this, I added the following code to my error page.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'rewrite url of the form "/user" or "/user/" to "/user/default.aspx"
Dim strMember As String = Request.ServerVariables("QUERY_STRING").ToLower.Replace(".aspx", "")
If Right(strMember, 1) = "/" Then strMember = Left(strMember, strMember.Length - 1) 'remove last "/"
Dim intPos As Integer = strMember.LastIndexOf("/")
If intPos > -1 Then strMember = Right(strMember, Len(strMember) - intPos - 1) Else strMember = ""
If strMember > "" Then Response.Redirect("http://www.samplesite.com/" & strMember & "/Default.aspx")
End Sub
If a member name is found in the requested URL, then a well-formed URL is generated and the visitor is re-directed to it.