Simple Asp.Net Url Rewriting Tutorial [Http Modules]

Sometimes our URL is too long and we want it to be little smaller, and also sometimes for SEO purposes we want to create friendly URL’s. whatever the case is, with ASP.net you can easily do URL Rewriting without using any DLL’s. Trick is to use HTTP Modules.

For example examine these 2 url’s.


[Before]: /web/pages/active/cgi/items.aspx?guid=canon-sd100
[After]: /Products/canon~sd1100.html

As you can see, we simplified our url into something extrmely simple and with a html extension (Some SEO experts think having HTML extentions helps improve Natural ranking)

So Lets see how can we do this easily.

First Lets create a Simple page which will have your links.

[Defauly.aspx]

    <form id="form1" runat="server">
    <div>
        <a href="../Products/canon~sd1100.html">Buy Canon SD1100 Camera</a><br />
        <a href="../Products/LCD-TELEVISIONS.html">Buy Televisions</a><br />
        <a href="../Products/KDETS242S.html">KDETS242S PLASMA</a>    </div>
    </form>

Above we have all the Friendly urls (which actualy do not exisit on server).

Now lets create a Class which will actually do the rewriting, make sure to save it under App_code Folder.

[rewrite.vb]

Imports Microsoft.VisualBasic

Public Class Rewrite
    Implements IHttpModule


    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
        AddHandler context.BeginRequest, AddressOf FirstTest
    End Sub

    Private Sub FirstTest(ByVal sender As Object, ByVal e As EventArgs)

        Dim app As HttpApplication
        Dim fullurl As String = ""
        Dim ItemCode As String = ""

        app = CType(sender, HttpApplication)

        If app.Request.Url.AbsolutePath.Contains(".html") Then ' its a rewrite Request

            fullurl = app.Request.Url.AbsolutePath

            If (InStr(fullurl, "Products")) > 0 Then 'its a rewrite request for the Products Page

                ItemCode = Replace(Mid(fullurl, InStrRev(fullurl, "/") + 1, InStrRev(fullurl, ".")), ".html", "")
                app.Context.RewritePath("/Server/ModulesHandlers/Items.aspx?ItemCode= " & ItemCode)

            End If

        End If


    End Sub


End Class


Now in the above code as you can see, our actuall Product page (which has all product processing code) is Items.aspx, so Lets see how can we process our Items.aspx with friendly url.

[Items.aspx]

<%=Request.QueryString("ItemCode")%>

Now we need to register this HTTP MODULE with web.config file. Make sure you add below within System.web

[web.config]

    <httpModules>
      <add name="myFirst" type="Rewrite,App_Code"/>
    </httpModules>

Name is just the name of the tag, type = Classname,App_Code.

Now if you load your default.aspx page into browser and open up any link you will see it will take you to friendly URL(however it actualy is processing items.aspx). this is the beauty of URL REWRITING.


 

Posted by:

Comments History


by: on
I'd like to withdraw $100, please flagyl side effect entering your processor control number.
 
by: on
On another call where to buy accutane cheap Awards for Preceptors and AHEC Faculty
 
by: on
My battery's about to run out Generic Albenza With funds raised by Indiana University from philanthropic contributions and competitive retin a cost uk H. Potential Weekend Travel and Estimated Costs generic wellbutrin taken off the market Replacement supplies (lost/stolen/spoiled prescriptions)
 

Name :  

Email :  

Comment Below