Saturday, March 5, 2011

URL rewriting using Global.asax in Asp.Net

There are lots of emerging technologies and Languages that come in existence to develop web sites. Some of them are PHP, JSP, Asp.net etc. Some web sites are static and some are Dynamic web site. Static Web site are Very Search engine Friendly because They have Static Hyperlink in their Content. I.e.: they don’t use any parameter to display there content. But in Dynamic Web Site Content depend on Parameter.
Unluckily, most of these databases driven web sites depends heavily on parameters to display contents since parameter passing is the easiest way to pass information between web pages.
However, most of the search engines cannot or will not list any dynamic URLS. Dynamic URLs are said to contain elements such as ?, &, %, +, =, $, etc. Hence, the pages which the hyperlinks point to will be ignored by the Web spider indexing the site.
But After the research on these dynamic websites and search engine mythology. There comes a new Concept of creating Search engine friendly urls for Dynamic Web site, called url rewriting. In this Concept we create Some virtual url. When they invoked then main url System check the url and take appropriate action.
This Article is using Asp.net (C#.net) technology. Use Global.asax file to implement this code on Application_BeginRequest. This event fire whenever Client browser make request for the site.
Main Goal of this Article is to Replaced Parameterized Url in to static URL.
We are replacing url like
http://www.Somedomain.com?bookid=81, url is not search engine friendly for better indexing.
Change it to
http://www.Somedomain.com?book81.aspx
First of All Some changes are to be made by you.
In your Global.asax file, modify the Application_BeginRequest event as the following,

protected void Application_BeginRequest(Object sender, EventArgs e)
{

HttpContext incomingRequest = HttpContext.Current;
string oldpath = incomingRequest.Request.Path.ToLower();
string bookid; // book id requested

// Regular expressions to grab the book id from the bookX.aspx
Regex regex = new Regex(@"book(\d+).aspx", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = regex.Matches(oldpath);

if (matches.Count > 0)
{
// Extract the book id and send it to ProcessBook.aspx
bookid = matches[0].Groups[1].ToString();
incomingRequest.RewritePath("ProcessBook.aspx?bookid=" + bookid);
}
else
// Display path if it doesn’t containt bookX.aspx pattern where x is an book id.
incomingRequest.RewritePath(oldpath);
}


In the above code, URLs of incoming requests are scanned to determine if the URL includes “bookX.aspx” , where X=1,2,3…n

Next create a file ProcessBook.aspx which would display book content on your web site based on the bookid parameter (bookid=1…n) just like you would do normally.

Ex.

Now You have to test this By Calling the page like http://www. Somedomain.com /book12.aspx

Here even though you are issuing a search engine friendly request, in the background you’re managing your parameters like you would normally do with parameters.This Method is implemented in Global.asax. You can Also create HTTPHandler Module To Track this.

No comments:

Post a Comment

Ajax CalendarExtender displaying at wrong position in Chrome

< script type ="text/javascript" language ="javascript">     function onCalendarShown(sender, args)...