For search engine optimization we need much cleaned urls for our web site. For example if we are creating any online shopping web site, we have to provide cleaned and meaningful urls for easy understanding for users and for search engines.
In this article we discuss about how to rewrite the urls in asp.net. For example we have some products page which displays products of particular category based on query string value. But displaying the category name in url as a query string like products.aspx?name=electronics is not user friendly, instead of that display url as products/electronics. For that we need to rewrite the url in Gloabal.asax file for user friendly display and internally url has to be original url to get the information from querystring.
Create asp.net web site and add two asp.net pages, name them as Default.asp, products.aspx. Add below content in Default.aspx page which is urls for products page.
<%@Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>URL rewriting Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="products/electronics">Click For Electronic Products</a><br/>
<a href="products/software">Click For Software Products</a>
</div>
</form>
</body>
</html>
As shown above, we provided the product urls as products/electronics and products/software. Internally we have to rewrite the urls as products.aspx?name=electronics and products.aspx?name=software in Global.asax. We can use Application_BeginRequest event of Global.asax file to rewrite the url as shown below.
void Application_BeginRequest(object sender, EventArgs e)
{
// Get current path
string CurrentPath = Request.Path.ToLower();
if (CurrentPath.StartsWith("/urlrewritingexp/products/"))
{
CurrentPath = CurrentPath.Substring(1);
string name = CurrentPath.Substring(CurrentPath.IndexOf("/") + 1);
//this is for local, remove it on production
name = name.Substring(name.IndexOf("/") + 1);
// Rewrite URL to use query strings
HttpContext MyContext = HttpContext.Current;
//this is to handle images, javascript and css files
if (CurrentPath.EndsWith(".jpg") || CurrentPath.EndsWith(".gif") || CurrentPath.EndsWith(".png")
|| CurrentPath.EndsWith(".css") || CurrentPath.EndsWith(".js"))
{
string path = "../../" + name;
MyContext.RewritePath(path);
}
else
{
MyContext.RewritePath("../products.aspx?name=" + name);
}
}
}
As shown above, we the requested url from user starts with /urlrewritingexp/products (where urlrewritingexp is application folder name, in server please remove that), we are redirecting the request to product.aspx page by passing the category name in query string. But user can see the url as products/electronics or products/software even though we are redirecting the request to products.aspx page.
Get the category name in products.aspx as shown below.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["name"] != null)
{
this.Title = Request.QueryString["name"].ToString();
lbl1.Text = Request.QueryString["name"].ToString();
}
}
Another advantage of url rewriting is even though you have single page for all categories, search engine assumes that separate page for each category. Because of that number of pages of your web site in search engine will increases and obviously your web site ranking also will increase.