Click here to Skip to main content
13,248,766 members (58,301 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 14 Nov 2007

Search Engine Optimization (SEO) and Friendly URLs using ASP.NET

, 20 Nov 2007
Rate this:
Please Sign up or sign in to vote.
Search Engine Optimization (SEO) and friendly URLs using ASP.NET


SEO (short for Search Engine Optimization) is the art, craft, and science of driving web traffic to websites.

Learning how to construct web sites and pages to improve "and not harm" the search engine placement of those websites and web pages has become a key component in the evolution of SEO.

In this article, we will see one of the techniques that is used commonly for improving SEO - Creating friendly URLs.

Today, most of the websites built are database driven or dynamic sites, and most of these websites pass data between pages using query strings. Search engine crawlers usually do not index the page having a question mark or any other character. If search engines do not identify a page or its content in a website, it essentially means missing web presence for that page. How can this be handled? This write-up discusses this topic with a sample website project as the implementation reference.

Friendly URLs

Friendly URLs pass information to pages without using the question mark or any other character, and these pages will be indexed by search engines, which will maximize search engine rankings for your website. Search engines prefer static URLs to dynamic URLs.

A dynamic URL is a page address that is created from the search of a database-driven website or the URL of a website that runs a script. In contrast to static URLs, in which the contents of the web page stay the same unless the changes are hard-coded into the HTML, dynamic URLs are generated from specific queries to a site's database. The dynamic page is only a template in which to display the results of the database query.

Search engines do not index dynamic URLs for the reason that it will contain non-standard characters like ?, &, %, = etc... Many times, anything after the non-standard character is omitted. For example, URLs like:

In this case, if the URL after the first non-standard character is omitted, the URL will look like:

URLs of this type will be a group of duplicate URLs for the search engine, and search engines omit duplicate URLs and will not index all your dynamic pages. A search engine indexes a URL like:

Even though nowadays search engines are optimized to index dynamic URLs, they would still prefer static URLs.

Creating SEO Friendly URLs

What if we were to implement this in our projects? Here's a method that could be used to create friendly URLs to boost your page rank.

In our example, let us try these URLs:

And our objective is to convert them to contextual URLs that resemble:

What we are going to do is that first we convert the actual URL with the query string to the contextual URL, by first getting the mapped URL name for the page from the web.config file, and combining the query string values followed by /, and finally, the item name as the page name. When this converted contextual URL is clicked and the page is navigated, the Application_BeginRequest event in the Global.asax file rewrites the contextual URL into the actual URL with the query strings.

Let's get down in detail about how to build the application that is using friendly URLs. This application simply explains how to create SEO friendly URLs and this is just an idea about how we can rewrite URLs. You can take the idea and rewrite it in your own way.

The GetContextualURL Method

This method takes actual URL that needs to be converted into a friendly URL and the name of the page as the parameters. Here in this application, we are using the "Order.aspx?Itemid=10&Item=Apple" URL as an example that needs to be converted.

The method will be called like:


In this method, the URL's query string values are being split and formed into a new URL with the existing page Order.aspx mapped to a new page named apple.aspx, and the alias name for Order.aspx is picked from the web.config file. The newly formed URL will be "~/shop/10/Apple.aspx".

The for loop in this method iterates through the appsettings of the web.config file and finds the key for the value "order.aspx". This is for assigning an alias name for the actual order.aspx. If you want more than one page to be converted, then you should have equivalent alias name entries in the web.config file.

public string GetContextualURL(string Url,string PageName)
    string DestID = string.Empty;
    string DestAlias = string.Empty;
    string Str = Url.Split('?')[1].Split('=')[1] + "/";
    string DestPage = Url.Split('?')[0];
    string ItemID = Url.Split('?')[1].Split('=')[1].Split('&')[0];
    string NewUrl = string.Empty;

    //Get all the Key/Value pairs from web.config
    string[] KeyList = WebConfigurationManager.AppSettings.AllKeys;

    //Iterate the collection to find the specfic key/value pair
    for (int KeyCount = 0; KeyCount < KeyList.Length; KeyCount++)
        DestID = WebConfigurationManager.AppSettings[KeyList[KeyCount]].Trim();
        DestAlias = KeyList[KeyCount];
        if (DestID.ToLower() == DestPage.Trim().ToLower())

    //Form the contextual URL
    NewUrl = "~" + DestAlias + ItemID + "/" + 
             PageName + ".aspx";

    return NewUrl;

Application_BeginRequest Event

This event is fired whenever a request is made to the server, and so this event is the appropriate event to rewrite the contextual URL into the actual URL. In this event, we are getting the incoming URL and checking whether it is a contextual URL, and if it is so, then we are splitting the item ID and the item name from the contextual URL and creating the actual URL with the query string. This actual URL gets processed and the actual page is executed.

protected void Application_BeginRequest(object sender, EventArgs e)
    //Get the current http context
    HttpContext InRequest = HttpContext.Current;

    //Get the current path
    string OldPath = InRequest.Request.Path.ToLower();

    //Check the path whether it is a contextual path
    if (InRequest.Request.RawUrl.Split('/').Length > 3)
        string Path = InRequest.Request.RawUrl.Split('/')[2];
        Path = "/" + Path + "/";

        string NewPath = "/furl/" + WebConfigurationManager.AppSettings[Path];
        string ItemName = InRequest.Request.RawUrl.Split('/')[
                            InRequest.Request.RawUrl.Split('/').Length - 1].Split('.')[0];
        string ID = InRequest.Request.RawUrl.Split('/')[3];

        //Rewrite the path with the actual path
        InRequest.RewritePath(NewPath, "", "?id=" + ID + 
                              "&id2=" + ItemName, true);

Requested Page

In the Page_Load event of the actual requested page, order.aspx gets the value of the item ID and the item name from the query string.

Label1.Text = Request.QueryString[1] +", Qty "+ Request.QueryString[0];


To implement Search Engine Optimization (SEO), there are numerous areas to focus - page index, strategies around maximizing page rank, etc... However, for a website with dynamic content, letting the page be indexed by search engines is the primary goal and a good beginning for SEO implementation.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mohan Kumar R
Web Developer
India India
Mohan Kumar is working as a Senior Developer and has about 5+ years of experience in the IT industry. He is a Microsoft Certified Technology Specialist in .Net Framework 2.0: Web Applications (MCTS).

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
mrdreamlife2-Jul-13 1:01
membermrdreamlife2-Jul-13 1:01 
Generalnice article Pin
gaurish thakkar13-Sep-12 19:22
membergaurish thakkar13-Sep-12 19:22 
QuestionApplication_BeginRequest Class is disturbing website at local server Pin
lavjain29-Jul-11 4:14
memberlavjain29-Jul-11 4:14 
Questionhow about just using spiderloop Pin
Prefict18-May-09 18:23
memberPrefict18-May-09 18:23 
QuestionFriendly URL is Not supporting in Shared hosting Servers [ &] Pin
Deepu M.I6-Mar-08 6:56
memberDeepu M.I6-Mar-08 6:56 
GeneralPoor approach and outdated Pin
Bert delaVega20-Nov-07 10:13
memberBert delaVega20-Nov-07 10:13 
GeneralAnother approach Pin
Bar Zecharya20-Nov-07 6:39
memberBar Zecharya20-Nov-07 6:39 
GeneralPoor Article Pin
Shakeel Hussain20-Nov-07 4:36
memberShakeel Hussain20-Nov-07 4:36 
GeneralPoor Strategy Pin
kckn4fun20-Nov-07 3:10
memberkckn4fun20-Nov-07 3:10 
GeneralArticle is better, than "no article at all" Pin
_Budda_20-Nov-07 2:57
member_Budda_20-Nov-07 2:57 
QuestionIs this an article ? Pin
N a v a n e e t h19-Nov-07 23:05
memberN a v a n e e t h19-Nov-07 23:05 
AnswerRe: Is this an article ? Pin
Mohan Kumar R20-Nov-07 1:36
memberMohan Kumar R20-Nov-07 1:36 
Questionhalf finished? Pin
Guy Harwood19-Nov-07 10:26
memberGuy Harwood19-Nov-07 10:26 
AnswerRe: half finished? Pin
Mohan Kumar R20-Nov-07 1:35
memberMohan Kumar R20-Nov-07 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Mohan Kumar R
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid