// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
Google / Android
Python / IronPython
SQL Server
Some quick and easy ways to rewrite urls
With ID as a query string parameter (...aspx?ID=...) being a big no-no from the search engine perspective, we would need a way to convert the convenient fake urls into the ones our system understands (like using ID= in the url). There are several ways of accomplishing this; we will look at a couple of quick options.
Table of Contents

If you don’t have a global.asax in your application, you can create one by choosing "Add New Item" and choosing "Global Application Class". This will provide the class template for handling application events (in file global.asax).

HttpApplication class from the .net framework forms the base class for global.asax. You can take a look at this class to see what events are supported by this class. What we are interested in is BeginRequest. At the beginning of every request we want to see if the current url is a fake one or whether it already exists for real in our system.

To determine if a given url is a fake url, we would need to follow some standards defined by us. For example, all our blogs might follow this standard:


To make sure this url is just a fake one, we should NOT have /blogs/ directory on our system (if you have one, you shouldn’t be putting anything that needs to be accessed from outside, of course). That way user could not be asking from some real files in the blogs directory.

So, by searching for the string /blogs/ in the incoming url, we will know that this is a fake url. Other examples could be:


In all these cases, we need to have simple string that couldn’t be pointing to a real directory (from the examples above: /employees/ or /products/ etc.) Once we determined that it’s a fake url, we need to make sense of the last piece of the url.

So, here the strings first-post-from-moon, bill-gates, and lawn-mower-123 should point to some id. Meaning we need to make the transformations like the following:

(to) mysite.com/showblog.aspx?name=first-post-from-moon
(or) mysite.com/showblog.aspx?ID=1234

(to) mysite.com/displayemp.aspx?id=23-34-34

(to) mysite.com/prods.aspx?prodid=lnmr_123

In these cases, the transformations can be done with the help of some predefined rules (like regular expressions) that convert the first string into the second one. Or, you can go to the database or an xml file to accomplish the conversion process. Start with adding BeginRequest event handler to the global.asax file:

/// <summary>
/// Executed at the beginning of every request.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Application_BeginRequest(object sender, EventArgs e)
    // We need to do a url rewrite if the original url contains
    // the word articles.
    string faker = @"/customers/";
    int idx = Request.Path.ToLower().IndexOf(faker);
    if (idx != -1)
        int idxAspx = Request.Path.ToLower().IndexOf(@".aspx");
        string fakeName = Request.Path.ToLower().Substring(idx + faker.Length, idxAspx - idx - faker.Length);
        // Prepare the new URL
        string realPage = "/Customers.aspx?Name=" + fakeName;
        string newPath = Request.Path.ToLower().Replace(faker + fakeName + ".aspx", realPage);
        // Rewrite the URL
        Context.RewritePath(newPath, false);

Bookmark and Share This

More Articles With Similar Tags
It's easy and convenient to obtain articles, blog posts, etc. via ID as query string. But the page generated with ID paramater has less of a chance of being indexed by search engine. Instead of ID, use a more search-engine-friendly name in the URL.
RewritePath used to service search engine friendly urls has problems with search engines. While this works fine with regular browsers, the request fails with the user agents of most all search engines. This article looks at the workarounds (fix from Microsoft was not available by early 2007)
Fiddler lets you capture the HTTP traffic on your computer very easily. You would be able to test how your site responds to requests from search engines very easily (by putting that user-agent string in the HTTP request). In addition, what’s discussed in this article can be used to debug things like the problems with RewritePath.
Transferring control from one aspx page to another is a pretty common need. This piece looks at one scenario.
About  Contact  Privacy Policy  Site Map