URL Rewriting Rules on the fly RRS feed

  • Question

  • User614557016 posted

    I want to implement the URL Rewriting in my website. Lets say I have a article website which have many articles sorted in different categories.

    To access any particular article URL looks like


    What I want is my url should look like this


    article_title and category name are both stored in database with corresponding article_id and cat_id. I don't have idea how to implement url rewriting rules on the fly.

    Though, I know how to configure web.config file. But that seems to be predecided rules based on some certain strings.

    Please help me out !

    Thanks and Regards



    Wednesday, April 5, 2006 7:53 AM

All replies

  • User-143727328 posted

    Take a look at this article here which should get you started. It also has links to third party products as well.



    Wednesday, April 5, 2006 8:53 PM
  • User614557016 posted

    Take a look at this article here which should get you started. It also has links to third party products as well.


    Thanks kevin,

    Article is really good, but i have confusion regarding implementation

    That is, imagine that a user types into their browser window, /people/ScottMitchell.aspx, which is rewritten to /info/employee.aspx?empID=1001.

    empID=1001 is related to ScotMitchell, that means, we will have to find the empid from database where employee name is ScottMitcheell and rewrite the url with empId fetched from url.

    In this article I didn't find the implementation. Infact I found no example on net where urlrewriting has been implemented using dynamic values fetched from database.

    I hope i make sense. It is really very important to me, please help


    Thursday, April 6, 2006 6:14 AM
  • User-1029435529 posted

    That is, imagine that a user types into their browser window, /people/ScottMitchell.aspx, which is rewritten to /info/employee.aspx?empID=1001
    That is not the way Scott's article works.  In the article, Scott uses regular expressions to rearrange the incoming URL.  For example, here is the incoming URL.


    Then, the regular expression code rearranges the URL:


    What is important to realize is that the ID of the page (friendly-urls) has not been changed.  It has only been rearranged in its position in the URL.  If you look at your own example that I have quoted, you will see that you have changed the ID from ScottMitchell to 1001.  That is not how the URL rewriting in Scott's article works, which is why the article does not talk about how to work with the database.

    You have two choices.

    First, you can stick with the technique shown in Scott's article, where you simply rearrange the incoming URL without ever hitting the database.  If your website is small, then this approach would be ideal.  It is simple and easy to use.  (I think the .sitemap file in ASP.NET version 2.0 allows for URL rewriting, so if you are using the new version of ASP.NET, then you might want to look into this possibility.)

    Second, you can use a completely different technique to that shown in Scott's article.  You'd still place the URL rewriting code in the same location as that shown by Scott, but you wouldn't use any regular expression-based rearranging.  Rather, you would pass the incoming URL to the database and retrieve the actual URL needed.  This is more complex than the approach taken in Scott's article, but it is a lot more flexible, and more appropriate to larger websites.

    I should point out that you would not really want to hit the database for every incoming page request.  Rather, you'd want to hit the database once to retrieve some sort of Site Map, and then use that loaded Site Map to figure out where to redirect all future incoming page requests.

    If you don't know which approach to take, then stick with Scott's approach.  Use it until you outgrow it, which may never happen.  So, use the URL rewriting just to rearrange the incoming page address.  Then, your page uses QueryString("id") to get the friendly ID of the article, or person, or product, or whatever else that page is to show.  Then, you can pass that friendly ID (such as "friendly-urls" or "ScottMitchell" or "iPod" or whatever) to the database in order to retrieve the details of the article or person or product.

    Does that help you?

    Thursday, April 6, 2006 10:01 PM