Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Jeff Prosise wrote a very interesting article in which he demonstrated how to rewrite URL using RewritePath method. In this post I will be using the same method to rewrite the article URLS. Good URL names can result in more traffic to the website since the URL's are index by the search engines. Let's say your website has the following Url:

http://www.mywebsite.com/ArticleDetails.aspx?id=10 

Although it works and performs the required option but it is very hard for the search engines to index the page since the URL does not give any indication of what it is about. Now, take a look at the following Url:

http://www.mywebsite.com/Articles/10_Introduction_To_C_Sharp_Programming

The URL above clearly conveys that it is an article about "Introduction to C Sharp Programming". Now, the search engine have more knowledge about the URL and hence it would appear in more searches driving more traffic to your website.

Let's first see how to display a list of articles whose URL's are changed at runtime.

 private void BindData()
        {
            GridViewGuyDBDataContext db = new GridViewGuyDBDataContext();
            gvArticles.DataSource = from a in db.Articles
                                    select new { ArticleID = a.ArticleID,
                                        Title = a.Title, Url = String.Format("Articles/{0}_{1}",a.ArticleID,
                                        a.Title.Replace(" ","_"))
                                    };                                       

            gvArticles.DataBind();
        }

The above code uses LINQ to SQL and anonymous types to customize the Url property. So, now the Url property would look something like the following:

Articles/{ArticleID}_{Title_of_the_article}

Keep in mind that I can add any folder I want. The Articles folder does not exists in reality.

Now, you want to redirect the requests for the above URL to the correct page (This page must exists in the real world). This is performed inside the Application_BeginRequest event which is fired whenever a user requests a new page.

 protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            string oldPath = context.Request.Url.ToString();
            string token = "Articles";
            int i = oldPath.IndexOf(token);

            if (i > -1)
            {
                int underScoreIndex = oldPath.IndexOf("_");
                int startIndex = (i + token.Length + 1);
                int numberOfCharacters = underScoreIndex - startIndex;
                int id = Int32.Parse(oldPath.Substring(startIndex,numberOfCharacters));
                context.RewritePath("~/ArticleDetails.aspx?id=" + id);
            }

        }

In the above code I am simply disecting the URL and getting the id out of the URL and then Rewriting the URL using the RewritePath method. So, when the user clicks on any article he will see a long URL which includes the ArticleID and Title but the page request will be sent to the ArticleDetails.aspx page. In ArticleDetails.aspx page you can easily extract out the query string (id of the article) using the following code:

if (Request.QueryString["id"] != null)
            {
                Response.Write(Request.QueryString["id"] as String);
            }

Off course, there are other ways of rewriting the URL but I find this simple. In the next post I will show you a more quicker way to re-write the URL.

Posted on Friday, January 25, 2008 8:34 AM | Back to top


Comments on this post: Url Rewriting Using RewritePath Method

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net