1.4 Generating User-Friendly URLs with UrlRewritingNet.UrlRewrite

ASP.NET applications often have the need to transfer data from one web form to another. In many cases, this is accomplished by passing QueryString variables in the URL. These URLs can become quite ugly, and, perhaps more importantly, search-engine spiders like the GoogleBot often don’t index them.

UrlRewritingNet.UrlRewrite is an open source component developed by Albert Weinert and Thomas Bandt that allows you to address this problem. Using UrlRewritingNet.UrlRewrite, you can easily turn your complicated URLs into easy-to-read, easy-to-remember, and easy-to-understand URLs. For example, the URL http://www.windevpowertools.com/ToolPage.aspx?ToolID=12 can become http://www.windevpowertools.com/Tools/12.

UrlRewritingNet.UrlRewrite works correctly with ASP.NET 2.0 features such as themes, master pages, cookieless sessions, output caching, and, most importantly, postbacks.

UrlRewritingNet.UrlRewrite at a Glance

Tool

UrlRewritingNet.UrlRewrite

Version covered

1.1

Home page

http://www.urlrewriting.net

Power Tools page

http://www.windevpowertools.com/tools/21

Summary

An open source library that enables you to rewrite URLs with ASP.NET 2.0

License type

Freeware

Online resources

http://www.urlrewriting.net/en/Config.aspx

Supported Frameworks

.NET 2.0

Getting Started

The good news is that you don’t have to set up anything on IIS. This makes it possible to use UrlRewritingNet.UrlRewrite in restricted hosting environments with low rights.

First, download the latest binaries from http://www.urlrewriting.net/en/Download.aspx. In the .zip archive, you’ll find three files:

  • UrlRewritingNet.UrlRewriter.dll

  • urlrewritingnet.xsd

  • Readme.txt

Copy the first file into your project’s bin folder (creating that folder in the root of your application if necessary), or add a reference to your web project as discussed in the Appendix. Then copy the second file anywhere in your project. After that, restart Visual Studio and reopen your project. In the simplest case, it should look like Figure 1-9.

The Visual Studio Project Explorer

Figure 1-9. The Visual Studio Project Explorer

Now open the web.config file in the root of your application and add the following lines inside the configuration section:

<configSections>
    <section
        name="urlRewritingNet"
        type="UrlRewritingNet.Configuration.UrlRewriteSection,
              UrlRewritingNet.UrlRewriter" />
</configSections>

<urlRewritingNet
    compileRegex="true"
    contextItemsPrefix="QueryString"
    rewriteOnlyVirtualUrls="true"
    xmlns="http://www.urlrewriting.net/schemas/config/2006/02">
    <rewrites>
        <add />
    </rewrites>
</urlrewritingnet>

Then put the following lines into your System.Web section:

<httpModules>
    <add
        name="UrlRewriteModule"
        type="UrlRewritingNet.Web.UrlRewriteModule,
             UrlRewritingNet.UrlRewriter" />
</httpModules>

Point your mouse arrow into the <add /> element that you added in the first step and hit the Space bar. You’ll see that you get excellent support from IntelliSense (see Figure 1-10).

IntelliSense Support in Visual Studio

Figure 1-10. IntelliSense Support in Visual Studio

Using URLRewritingNet.UrlRewrite

Now you’re ready to rewrite some URLs! Let’s add two very simple example rewrites to demonstrate the tool’s functionality.

Imagine you have a page that displays a news article. The ID for the news article in your database comes in QueryString format: /News.aspx?ArticleID=10.

Wouldn’t it be nice if the URL looked like /News/Article10.aspx? You can make this happen by adding a rewrite rule for this URL to your rewrites section:

<add
    virtualUrl="~/News/Article(\d+).aspx"
    destinationUrl="~/News.aspx?ArticleID=$1"
    ignoreCase="true" />

The regular expression in the virtualUrl property looks for any page called Article with a number at the end. The number specified is then passed to the destinationUrl property in place of the $1 variable. Multiple matches can be handled by simply incrementing the variable number for each of the matches you want to pass to the destination URL:

<add
    virtualUrl="~/News/(\d+)/Article(\d+).aspx"
    destinationUrl="~/News.aspx?CategoryID=$1&ArticleID=$2"
    ignoreCase="true" />

Now, what if you want to redirect a request permanently? This is useful if you have an outdated page with a high Google Page Rank that you want to transfer to another page. To permanently redirect the requests to the old page (HTTP 301), just define a rule like the following:

<add
    virtualUrl="~/Old.aspx"
    redirect="Application"
    destinationUrl="~/NewPage.aspx"
    redirectMode="Permanent" />

Getting Support

If you have any problems setting up UrlRewritingNet.UrlRewrite or configuring the rules, take a look at the project’s home page (http://www.urlrewriting.net). You’ll find a detailed list of all possible configuration options, a list of frequently asked questions, and the project support forums.

Get Windows Developer Power Tools now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.