You are in: Home > Articles > Search Engine Friendly URLs using ASP.NET (C#.NET)

Search Engine Friendly URLs using ASP.NET (C#.NET)

Each year companies and individuals spend millions of dollars on websites that depend on databases to offer dynamic content. Yet these sites never receive the exposure they deserve. Database driven sites are used because their administration is much easier than the sites with static content. Usually, all it takes to update the contents of a database driven web site it making changes to certain tables in the database. Unfortunately, most of these database driven web sites rely heavily on parameters to display contents since parameter passing is the easiest way to pass information between web pages. Yet, most search engines cannot or will not list any dynamic URLS. Dynamic URLs are said to contain elements such as ?, &, %, +, =, $, cgi-bin, .cgi. Hence, the pages which the hyperlinks point to will be ignored by the Web spider indexing the site.

I have been searching many top search engines for information on creating a search engine friendly web site using ASP.NET (C#. NET) and have not been able to find anything. So, I thought it would be a good idea to create an article and share it with the rest of the world.

Goal

Instead of sending parameters like

http://www.mydomain.com?pageid=4 which is not search engine friendly

we want to change it to

http://www.mydomain.com/page4.aspx

Prerequisites

In your Global.asax file, modify the Application_BeginRequest event as the following:

 
protected void Application_BeginRequest(Object sender, EventArgs e)
{
       
	HttpContext incoming = HttpContext.Current;
	string oldpath = incoming.Request.Path.ToLower();
	string pageid; // page id requested
      
    // Regular expressions to grab the page id from the pageX.aspx
    Regex regex = new Regex(@"page(\d+).aspx", RegexOptions.IgnoreCase 
    | RegexOptions.IgnorePatternWhitespace);
    MatchCollection matches = regex.Matches(oldpath);
	
    If (matches.Count > 0) 
	 {
        // Extract the page id and send it to Process.aspx
        pageid = matches[0].Groups[1].ToString();
        incoming.RewritePath("Process.aspx?pageid=" + pageid); 
	 }
    else
    {
	 // Display path if it doesn’t containt pageX.aspx
	   incoming.RewritePath(oldpath 
} }

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

Your code

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

Ex.

<%
string pageid = Request.QueryString["pageid"];
// Create the page content based on this pageid taken here
%>

Finally

Issue a test request

http://www.yourdomain.com/page12.aspx

Done

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.


By Maziar Aflatoun