Bookmark This Site

Search engine friendly URLs using ASP.NET (C#.NET)

Search engine friendly URLs using ASP.NET (C#.NET)

Author: Maziar Aflatoun
Submission Date: 2006-04-24
Website:
Email:

Overview

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 content of a database driven web site is to make changes to certain tables in the database. Unfortunately, most of these database driven web sites rely heavily on parameters to display content 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 write an article and share my experience 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.

<%

string pageid = Request.QueryString[\"pageid\"];

// Create the page content based on this pageid taken here

%>

Finally

Issue a test request to http://www.yourdomain.com/page12.aspx.

Done

Here even though you are issuing a search engine friendly request, in the background you're accessing your parameters like you would normally do.

Rate this article: Lowest = 1 to Highest = 5
View PDF   Bookmark   Printer Friendly

[ ^Top ]    [ Go Back ]