URL Rewriting (Part 3)

This is offcourse linked to the last blog for URL Rewritting, some technical bits to be shared, a little enhancement to the previous technique to URL Rewriting and also an issue which could be a limitation (or more likely that a I'm unable to findout solution to it) for the way I implemented the URL Rewritting.

I had implemented the URLs with a directory structure strategy.
i.e.  http://localhost:3648/Teams/
       http://localhost:3648/Teams/Pakistan/
       http://localhost:3648/Teams/India/
       http://localhost:3648/Teams/Sri-Lanka/

While going into live environment, I faced an issue and which (so far) I'm unable to resolve...

The issue was that when the page was redirected to a URL (say http://localhost:3648/Teams/Pakistan/)  the images to be displayed in the page took the different relative path for the image URL and hence not displaying the picture.

For example:
If the image path was "images/testimage.jpg" then after URL Rewritting, it became as "Pakistan/images/testimage.jpg" and therefore misguiding the browser to retreive the image from a non-existent and wrong directory...

This led me to change my strategy towards URL Rewritting technique, and I rather switched to a slighly different URL Rewritting bit.

i.e.  http://localhost:3648/Teams.aspx
       http://localhost:3648/Pakistan.aspx
       http://localhost:3648/India.aspx
       http://localhost:3648/Sri-Lanka.aspx
       http://localhost:3648/Hockey.htm
       http://localhost:3648/Tennis.shtml

From the SEO (Search Engine Optimization) point of view, this is still very useful URL.

I had to do the following change in the "Application_BeginRequest" event in global.asax file

Old Code:

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim strPath As String = Request.Url.ToString()
        If strPath.Contains("Pakistan/") Then
            Context.RewritePath("~/teampage.aspx?TeamID=1")
        ElseIf strPath.Contains("India/") Then
            Context.RewritePath("~/teampage.aspx?TeamID=2")
        ElseIf strPath.Contains("Sri-Lanka/") Then
            Context.RewritePath("~/teampage.aspx?TeamID=3")
        ElseIf strPath.Contains("Cricket/") Then
            Context.RewritePath("~/sportspage.aspx?SportID=1")
        ElseIf strPath.Contains("Football/") Then
            Context.RewritePath("~/sportspage.aspx?SportID=2")
        ElseIf strPath.Contains("Tennis/") Then
            Context.RewritePath("~/sportspage.aspx?SportID=3")
        ElseIf strPath.Contains("Hockey/") Then
            Context.RewritePath("~/sportspage.aspx?SportID=4")
        Else
            Context.RewritePath("~/default.aspx")
        End If
    End Sub

New Code:

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim strPath As String = Request.Url.ToString()
        If strPath.Contains("Pakistan.aspx") Then
            Context.RewritePath("~/teampage.aspx?TeamID=1")
        ElseIf strPath.Contains("India.aspx") Then
            Context.RewritePath("~/teampage.aspx?TeamID=2")
        ElseIf strPath.Contains("Sri-Lanka.aspx") Then
            Context.RewritePath("~/teampage.aspx?TeamID=3")
        ElseIf strPath.Contains("Cricket.aspx") Then
            Context.RewritePath("~/sportspage.aspx?SportID=1")
        ElseIf strPath.Contains("Football.aspx") Then
            Context.RewritePath("~/sportspage.aspx?SportID=2")
        ElseIf strPath.Contains("Tennis.shtml") Then
            Context.RewritePath("~/sportspage.aspx?SportID=3")
        ElseIf strPath.Contains("Hockey.htm") Then
            Context.RewritePath("~/sportspage.aspx?SportID=4")
        End If
    End Sub

Also, with the changes in the URLs i.e. Hockey.htm or India.aspx, you may also had noticed that I've removed the else condition, that was because, while when the browser is reading the image file, this function is called and the control directly goes to the else condition, while reading the image, and that lead the image URL to parse as default.aspx

 Following is the snapshot of the application with the folder technique

Following is the new snapshot of the application with the URL (without folder)


With this strategy also, the limitations remain the same as discussed in previous post.
But as far as SEO is concerned this way or the previous one, we can acheive our targets.

The Server.Execute or Server.Transfer methods for instance, can be utilized at places where necessary but they would definetely show up with the original URL which was masked (re-written) but since the Top Level Menus of the website are using "response.redirect" method, it will still serve the purpose.

The project is the same consists of the following files

Global.asax
MasterPage.master
default.aspx
teampage.aspx 
sportspage.aspx
web.config

The master page contains the menus (Four link buttons) each of which redirects to a different page (A dummy URL).

OLD Example Code:

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Response.Redirect("~/Cricket/")
    End Sub

    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
        Response.Redirect("~/Football/")
    End Sub

    Protected Sub LinkButton3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton3.Click
        Response.Redirect("~/Tennis/")
    End Sub

    Protected Sub LinkButton4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton4.Click
        Response.Redirect("~/Hockey/")
    End Sub

New Example Code:

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Response.Redirect("~/Cricket.aspx")
    End Sub

    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
        Response.Redirect("~/Football.aspx")
    End Sub

    Protected Sub LinkButton3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton3.Click
        Response.Redirect("~/Tennis.shtml")
    End Sub

    Protected Sub LinkButton4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton4.Click
        Response.Redirect("~/Hockey.htm")
    End Sub

------------------------

Although, this is one other way for implementing the URL re-writting, I'll will welcome to some one of you would find a solve to it to help me out
as well as I will be searching for it, and will update you if I find some resolution to that.

The change I did in the existing project can be downloaded by clicking URL_TEST_updated.zip
------------------------

There are other ways for the URL rewriting two, while IIS7 provides much better functionalities supporting the pageless URL re-writing

You can have a look at ScottGu's Blog to have an indepth view of what other ways are there for URL re-writing as well.
Othere Useful Links are
http://www.asp101.com/articles/matteo/urlrewriting/default.asp
http://www.mattcutts.com/blog/asp-net-2-and-url-rewriting-sometimes-harmful/

Cheers !

raheel Hussain


 

Published Wednesday, October 03, 2007 4:13 AM by Raheel Hussain

Comments

# re: URL Rewriting (Part 3)

Tuesday, October 09, 2007 11:46 AM by LanceUSA

I have devised a solution for your issue.

Use regular expessions in the Global.asax Application_BeginRequest to Rewrite any urls that come though to point to the root.  I'm still testing it out, but thought you might be interested.

One caveat I found was with rewriting the paths of webservices.  I eventually found a solution here: forums.asp.net/.../1113541.aspx.  Essentially you just treat the context.RewritePath differently for webservices based on the code found at that url.

The good side of this is not my AJAX Update Panels work with some url rewritting logic.  Before I was using urlrewrittingnet and was getting errors in any postback ussued within an update panel.

Thanks for writing the article to ge me started!

# re: URL Rewriting (Part 3)

Tuesday, October 09, 2007 3:31 PM by LanceUSA

One other thing you might be interested in...I didn't like the fact that urls thought the root was the rewritten url...to solve this is quite easy in javascript.

If you're using asp.net ajax its as simply as doing this in the client pageLoad function

for(var i = 0; i < document.links.length; i++) {

    document.linksIdea.href = document.linksIdea.href.replace(/regex/, "");

}

Now all my hyperlinks point to my application root!

I noticed you still need the actionform from scottgu's blog to make sure the action retains the rewritten url.

Now I've got a complete urlrewritting setup!

Nice!

# re: URL Rewriting (Part 3)

Thursday, October 18, 2007 5:59 PM by Raheel Hussain

thanks,  I will definetely look into it.

Powered by Community Server (Non-Commercial Edition), by Telligent Systems