Wednesday, March 23, 2011

Tricks for URL Rewriting using Request.PathInfo in Asp.Net

To publish cleaner URL in your Asp.Net application you can use URL Rewriting. The most popular reason for URL Rewriting is let you have implemented pages based on query string like http://yourdomain/product.aspx?Brand=Lux. But now you want to change the URL to http://yourdomain/product.aspx/Lux for SEO purpose. There is a lot of way to implement this. One of the easy trick is you can achieve URL Rewriting by using Request.PathInfo of Asp.Net. Mind it this is not the best way for a huge site because this technique requires some of coding which is error prone. One another thing that if you have 10 brands & you use query string to populate Brand related product then google will index only one url but if you use URL like http://yourdomain/product.aspx/Lux then google will treat all ten individual pages. Nice thing am i right? Another reason for URL Rewriting is to server the pages those users have bookmarked old query string URL's. Let’s move for implementation. To do that first add a page in your project and named it BrandList.aspx.



Add a button in this page and write the below code under button click event:
Response.Redirect("Product.aspx/Lux");

Now add another page and name the page product.aspx. Now write the following server side code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Response.Write("Brand=" + GetBrand());
            // Now you can do anything sinch you have got the BrandName
        }
    }

    public string GetBrand()
    {
        if (Request.PathInfo.Length == 0)
            return Request["Brand"];
        else
            return Request.PathInfo.Substring(1);
    }

Now run the BrandList page and click on the button. Check your browser address you will find a SEO friendly URL. The previous bookmark users also get your product list.

1 comments:

pharmacy said...

Hey thanks for posting this! I was looking for it weeks ago. It will be very helpful to me.

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash