Sunday, August 22, 2010

Url Rewriting Made Easy - Article 1

Rewriting of URL is one of the Major point in terms of Search Engine Optimization as well as in terms of Readability and usability. 
We should go for rewriting of URL because of following reasons
  1. Search Engines Recognizes different URLs.
  2. Same Page can be executed with different unique URLs.
  3. We can also introduce our keywords in the URLs.
In this article we will discuss very preliminary way to generate unique URL which internally can target to same page. We will also discuss how to maintain the URL during postbacks. We have to consider postback case because though we manage to redirect to some URL which is rewritten, during postbacks it again changes to the actual URL.
 
In general Our URLs looks like below
http://www.yourSite.com/products.aspx?category=Kids
http://www.yourSite.com/products.aspx?category=Science
http://www.yourSite.com/products.aspx?category=Biology
We are going to use a less used feature of Asp.Net i.e. "Request.PathInfo" using which we will able handle
URLs which are as below
http://www.yourSite.com/products.aspx/Kids
http://www.yourSite.com/products.aspx/Science
http://www.yourSite.com/products.aspx/Biology
This is one of the simplest approach to Handle URL Rewriting.  Now Lets start implementing it, before we go ahead check out the folder structure of the DemoCode for this article which is as below.
Lets Discuss one File at a time
  • Default.aspx: This file contains simple Div Tag with 3 links point to Product.aspx with desired URLs. Check out following code for the same. Check out URLs.



  • Product.aspx: HTML of Product.aspx contains following simple code.


    code behind of Product.aspx is as follows








    protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.PathInfo.Substring(1))
            { 
                case "Kids":
                    Response.Write("This is Kids Section");
                    break;
                case "Science":
                    Response.Write("This is Science Section");
                    break;
                case "Biology":
                    Response.Write("This is Biology Section");
                    break;
            }
    
            Response.Write("
    
    
    Check out the URL");
        }
        protected void Button_Click(object sender, EventArgs e)
        {
            Response.Write("URL is Not Changing After PostBack");
        }

    This is very simple way to implement and understand which category is being called. upto this point code can work very well. But to make sure that on post back also i.e. on button click on Product.aspx page same URL should be maintained, we have to write Control Adapter which we have written in "HandlePostBacks.cs" file
  • HandlePostBacks.cs:  In this file we have overriden Render function of Control Adapter class to rewrite the Raw URL during postbacks. check out following code.











    public class HandlePostBacks : System.Web.UI.Adapters.ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(new RewriteForPostBack(writer));
        }
    }
    
    public class RewriteForPostBack : System.Web.UI.HtmlTextWriter
    {
        public RewriteForPostBack(HtmlTextWriter writer) : base(writer)
        {
            this.InnerWriter = writer.InnerWriter;
        }
    
        public RewriteForPostBack(System.IO.TextWriter writer) : base(writer)
        {
            base.InnerWriter = writer;
        }
    
        public override void WriteAttribute(string name, string value, bool fEncode)
        {
            if (name == "action")
            {
                HttpContext context;
                context = HttpContext.Current;
                value = context.Request.RawUrl;
                       }
            base.WriteAttribute(name, value, fEncode);
        }
    }

    Now we have almost done with our logic, now what remains is to register above adapter so that it get called automatically. For this we will write logic in "postback.browser" file

  • Postback.browser: We write simple logic which tells .net which adapter control should be used, check following code.



    
        
          
        
      

And we are done with the implementation DOWNLOAD demo code and check it out, very simple and can implement easily on other pages.
Download Demo Code here --> UrlRewitting-Simple Approach 1.zip
Submit this story to DotNetKicks

2 comments:

Dorababu May 10, 2012 at 4:52 PM  

How to rewrite the URL on button click event.. Assume I am redirecting to a page on button click then how can I rewrite that URL

writer May 31, 2012 at 10:18 AM  

@Dorababu; you will have to write your own logic to match your URL rewrite pattern.