Friendly URLs in ASP.NET using URLRewriter.NET

I was having a chat the other day with Danish Peter (yes, another one) about URL rewrites in ASP.NET. In a previous post I wrote about how to use friendly URLs in the Zend framework, in this post, which is based on our discussion, I will discuss how to do this in ASP.NET.

Friendly URLs are achieved by a technique called URL-rewrite. A friendly URL is a URL that is easy to read and understand. Let me give you an example (from a site my girlfriend loves) of what is NOT a friendly URL:

This might be an extreme example, but there are plenty of sites which are as bad. If the above URL was transformed into a friendly URL it might look something like:

The second address is easier to read. URL rewrite techniques have been around for a long time and the first time I used it was back in the early Apache 1.3 days. Today good sites use friendly URLs and if you have not adopted it yet, you need to start using it.

There are several benefits from using a friendly url scheme, the most prominent are:

  1. 1. The clear structure makes it easier for humans to read and understand the addres
  2. 2. It is easier for search engines to understand what the address is and crawler the site. URL structure that goes more than three directories deep is not always read by a spider.
  3. 3. If search engines understand the URL, it is also easier for them to help people finding what they want, giving better search results. Hence using keywords and proper titles makes life easier.

What options are there for ASP.NET?
There is no built in support for having friendly URLs in ASP.NET, but you could spend your time writing your own URL-rewrite module. Writing your own module is basically spending time reinventing the wheel. If you have the time, go ahead, but if not there are already several good ones out there (free and open source). One that I have found very useful is the URLRewriter.NET ( URLRewriter.NET provides similar rewrite capabilities as the mod_rewrite does for Apache.

Using URLRewriter.NET
It is possible to use the URLRewriter.NET module without making any changes to the IIS configuration, this will however limit the functionallity the URLRewriter.NET rewriting capabilities. To use its full power, IIS should be configured to map all requests to the ASP.NET runtime (how to do this is covered in the manual). We will only be touching the surface of URLRewriter.NET so we will not have to do any modifications to the IIS configuration.

To add URLRewriter.NET to your project start out by opening Visual Studio or Visual Web Developer. Open your project and then right click the project in the solution explorer and click on Add Reference, go browse and find the folder you downloaded and unzipped. You will find the binary (.dll) file in “Loation”\urlrewriternet20rc1b6\UrlRewriterV2\bin\Release.

Before we can use the module we need to make some changes to the web.config. Add the following in the configSections:


This will allow the URL writer to read the rewrite rules we will define later. Now we need to add this module to the httpModules section:


By adding the rewriter as a httpModule we allow it to intercept web requests and route the requests to the actual aspx file serving the page. Now we need to define the rewrite rules:


The rewrite rules are defined using regular expressions. Regular expressions are very powerful and is something every programmer should know. The syntax might be hard to remember but there are several great tools to help build advanced regular expressions, for example the RegexDesigner.NET ( that Chris Sells has created.

The above rewrite rules are simple. The first rewrite rule will rewrite URLs like


And the second rewrite rule will match requests starting with ~/Article/ so it will rewrite URLs like


Finishing up
By now you hopefully have a functioning site that uses friendly URLs. One of the common problems you might run into is broken links. To avoid this you should make sure that you reference all your pages from the root level (~) and that all your links has the runat=server tag.

Thanks for your input Peter! You can check out his Danish Peter blog over at Magic Mouse.


9 thoughts on “Friendly URLs in ASP.NET using URLRewriter.NET

  1. Hi Peter,

    I got my MSSQL database running on the web server of the hosting company. I tried to implement what you said on the database in my laptop – it worked!

    However, the feature does not work on the actual website even after I uploaded the urlrewriter.dll and another xml file into the bin folder of the hosting server. I am suspecting that it might be due to the fact that I had “add reference” to the urlrewriter.dll in my local machine, but not the one at the hosting server. Is there anyway to “add reference” for the database hosted by hosting company’s server? Thanks.

  2. Have you modified your web.config on your hosting web-server? You need to add references to URLRewriter as described in the article. Also, I assume that locally you are using the WebDev server that is built into Visual Studio? This webserver behaves slightly different from IIS, in WebDev all requests gets routed to the ASP.NET runtime, while in IIS this is not the case. I think you can find instructions for how to configure this for IIS 6 in the URLRewriter documentation.

  3. hi,

    i have a url like


    and this should be redirected to the url like


    can you please help me how to write the rule for this type of links and the id’s and destination region name(stratford-upon-avon) are dynamic.

  4. In order to create that rewrite you need to create some kind of mapping between the name “stratford-upon-avon-coach-holiday” and CountryId=104, RegionId=155 and SubRegionId=566, which will be a bit more complicated. An easier solution would be to create an URL like:


    Not as nice as the url you suggested, but the url will contain the information you need to do the rewrite. With this URL you can create a rule like:

    rewrite url=”~/([0-9]+)-([0-9]+)-([0-9]+)/(.+)aspx” to=”~/travelguide_details.aspx?CountryId=$1&RegionId=$2&SubRegionId=$3″/

    I hope this helps!

  5. hi,
    i wrote this

    for this output http:~/cheapflightsto-accra-7-H.aspx

    its working fine localy in and in IIS as well
    but when i upload, it gives error i.e.
    HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /cheapflightsto-accra-7-H.aspx

  6. Hi Sohaib,

    It seems like the rewrite module isn’t loaded by IIS. Have you uploaded the UrlRewriter dlls and made the changes to web.config on the server? Are you using the same version of IIS on your local dev machine as on the server? If your server is running IIS7 the web.config has changed and you cannot use the example I pasted in this post.


    How Are You

    How this Code Works on IIS 7
    AND Visual Studio 2008
    Target FrameWork 2.0

    Because It Gave Me Error
    Resource Not Found

    and Thank You In Advance

Leave a Reply

Your email address will not be published. Required fields are marked *