Lately, topics like search engine optimization and writing user-friendly URLs are to find everywhere on the Internet. Particularly, rewritten URLs make it a lot easier to embed topic-specific or product-specific keywords into the URLs of the pages on your website which increases accessibility for both your audience and for various web crawlers. This is accomplished by moving away from typical query string arguments like for e.g.:
http://nedzadsmajic.info/?p=11&preview=true&a1=more&a2=argu&a3=ments&a4=0&a5=1&a6=2&a7=etc to a more readable form like: http://nedzadsmajic.info/2008/06/04/url-rewriting-for-aspnet.

After evaluating several URL rewriting engines, the component of my choice was the open source component called “UrlRewritingNet.UrlRewrite”, because it was really easy to implement.

Functions of UrlRewritingNet.UrlRewrite are:

  • Rewriting URLs based on regular expressions
  • Support for ASP.NET themes and master pages
  • Support for output caching
  • Use in medium trust level environments (shared hosting) possible
  • Consistent URL after post back
  • Possibility to add your own rewrite rule providers
  • Redirects (also permanent) to other domains or websites possible
  • Support for cookie-less sessions
  • Adding rewrite rules on runtime
  • Easy installation and use

And there are some drawbacks too:

  • No ASP.NET 1.x support
  • Rewritten URLs must end with an extension (although, it can be configured under IIS ISAPI settings)
  • Cross-page posting won’t work any longer

To use the “UrlRewritingNet.UrlRewrite” component, follow these steps:

Configuration schema changes
Add following entries to your web.config file:

<configSections>
    <section name="urlrewritingnet"
             restartOnExternalChanges="true"
             requirePermission ="false"
             type="UrlRewritingNet.Configuration.UrlRewriteSection, 
                   UrlRewritingNet.UrlRewriter"  />
</configSections>

To handle all incoming requests with UrlRewritingNet you have to register the component as Http Module in the section:

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

Finally, you have to add the rewriting rules. It is recommended to write them as specific as possible, for example not “^~/(.*)/(.*).aspx”:

<urlrewritingnet
   rewriteOnlyVirtualUrls="true"
   contextItemsPrefix="QueryString"
   defaultPage = "Default.aspx"
   defaultProvider="RegEx"
   xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
    <!--URL Rewriting Section-->
    <rewrites>
	<!--products-->
      <add name="ProductsPageRule"
         virtualUrl="^~/product/([a-zA-Z][\w/_-]{1,149})_(.+).aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Products.aspx?id=$2&amp;ot=S&amp;pageName=O"
         ignoreCase="true" />
      <!--authors-->
      <add name="AuthorsPageRule"
         virtualUrl="^~/author/([a-zA-Z][\w/-]{1,149})_(.+).aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Authors.aspx?id=$2&amp;ot=N&amp;pageName=O"
         ignoreCase="true" />
      <!--categories-->
      <add name="CategoriesPageRule"
         virtualUrl="^~/category/([a-zA-Z][\w/-]{1,149})_(.+).aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Categories.aspx?id=$2&amp;ot=R&amp;pageName=O"
         ignoreCase="true" />      
    </rewrites>
  </urlrewritingnet>

Explanation of the terms:
virtualUrl – A regular expression which is used for replacing the current URL with destinationUrl.
destinationUrl – A regular expression term describing the target page (physical file).

Tip #1:

If you’re going to use more comprehensive rules, it’s a good practice to move them to a separate configuration file.

Finally, rewrite your respective URLs so they conform to the rewriting rules.