March 9, 2021
Hot Topics:

How to Implement and Utilize URL Rewriting with ASP.NET

  • By David Consdorf
  • Send Email »
  • More Articles »


Smart URL rewriting can add usability and professionalism to any web site. It is also an important factor in helping web sites control how they are indexed by search engines. In this article, I will walk you through a simple implementation of URL rewriting for ASP.NET and some solutions to common problems. I will also discuss why URL rewriting is important and how URL rewriting can be used to enhance your web site.

What Is URL Rewriting?

URL rewriting is the process of taking an incoming URL request and rewriting it to another URL that the web site can process. For example, if the URL comes in through the browser as "www.mysite.com/UserProfile/1.aspx" it may be rewritten to another URL like "www.mysite.com/UserProfile.aspx?ID=1" that your site can better read.

The ability to rewrite URLs is quite useful because it allows you to do things such as improve search engines' ability to read and index your site, change your web site structure without requiring users to change their bookmarks or other web sites to change their links, improve your web site's security, and generally making your site more usable and professional. I will go over each of these items in more detail the "How to Utilize URL Rewriting" section of this article.

How to Implement URL Rewriting with ASP.NET

URL rewriting can be implemented programmatically. The Context.RewritePath() method allows you to programmatically rewrite the processing path of an incoming URL. Once rewritten, the system will continue executing the request using the new path.

In the Application_BeginRequest() method of the Global.asax file, you need to add code to read the incoming path, match it to one or more URL rewriting rules, and then rewrite the path for further processing. See the example below; it implements the following URL rewrite rules:

Input URL Actual URL
~/UserAccount/OldUrl.aspx ~/UserAccount/NewUrl.aspx
~/UserAccount/{UserID}.aspx ~/UserAccount.aspx?ID={UserID}

Table 1: Context.RewritePath() Example: URL Rewrite Rules

void Application_BeginRequest(object sender, EventArgs e)
   String path = Request.Url.ToString();

   if (Regex.IsMatch(path, "/URLRewriting/OldUrl.aspx",
   else if (Regex.IsMatch(path, "/URLRewriting/UserAccount/(.+).aspx",
      String idString =
         path.Substring(path.LastIndexOf('/') + 1,
                        path.Length - path.LastIndexOf('/') - 6);
         Context.RewritePath("/URLRewriting/UserAccount.aspx?id=" +

Listing 1: Using Context.RewritePath() to perform URL rewriting

In this example, every time a new request is processed it will first run through the Application_BeginRequest(). The URL path input is retrieved using the Request.Url property and then the web site URL rewrite rules are applied by using regular expressions to match against expected inputs and to rewrite them as paths to the pages in your site.

A More Robust Example Using UrlRewriting.NET

Using the Context.RewritePath() method to programmatically rewrite URLs works fine when done on a small scale with only a limited set of rewrite rules, but larger sites often will have many rules for URL rewriting. Coding all of those rewrite rules manually can be a cumbersome and error-prone task.

A better solution is to use an HttpModule to dynamically handle URL rewriting rules from the web.config file. Instead of writing the HttpModule to handle this task yourself, there are several good free versions already out there. Here are a few of them:

For this article, I will walk you through a simple example using URLRewriting.Net. To begin using URLRewriting.Net, first download the binaries from the URLRewriting.Net web site and put them in your websites/bin folder.

Next, add the following configuration settings to the web.config file:

   <section name="urlrewritingnet" requirePermission ="false"
            UrlRewritingNet.UrlRewriter"  />

   <compilation debug="true"/>
   <authentication mode="Windows"/>
      <add name="UrlRewriteModule"
                 UrlRewritingNet.UrlRewriter" />

Listing 2: Configuring URLRewriting.Net

Page 1 of 4

This article was originally published on October 8, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date