URL Rewriting with custom, open source or commercial Rewriter DLLs

Description
Table of Contents
Description

These components are implemented as HTTP modules that hiddes the use of the “HttpContext.RewritePath()” method that ASP.NET provides and uses rules configuration in order to support dynamic URLs with different and multiple parameters in the querystring.

How it works

The re-writing process is performed automatically at the beggining of the request pipeline according the re-write rules configured in the “web.config” file.

The HTTP module URL rewriters usually also add support for regular expression and URL pattern matching (to avoid having to hard-code every URL in your “web.config” file).
The extension on the original URL is not required only on II7.

Operations
Rewriting with the Intelligencia.UrlRewriter.Net DLL

The configuration section is included within the “web.config” file.

How to do it
<configuration>

  <configSections>
    <section name="rewriter"
             requirePermission="false"
             type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
  </configSections>

  <!-- Configurtion for II6 and prior versions -->
  <system.web>

    <httpModules>
      <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
    </httpModules>

  </system.web>

  <!-- Configurtion for II7 -->
  <system.webServer>
		<validation validateIntegratedModeConfiguration="false" />

    	<modules runAllManagedModulesForAllRequests="true">
			<add name="UrlRewriter"
				type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" />
    	</modules>

    </system.webServer>

  <rewriter>
    <rewrite url="~/products/books.aspx" to="~/products.aspx?category=books" />
    <rewrite url="~/products/CDs.aspx" to="~/products.aspx?category=CDs" />
    <rewrite url="~/products/DVDs.aspx" to="~/products.aspx?category=DVDs" />
	<rewrite url="~/products/(.+).aspx" to="~/products.aspx?category=$1" />
  </rewriter>  

</configuration>
Rewriting with the UrlRewritingNet.UrlRewriter DLL

The rewriting rules is configure on an external “xxx.config” configuration file and the configuration file is set on the “web.config” file.

How to do it
<urlrewritingnet xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
  <rewrites>
    <!--
				Urlrewriting.Net is a cool tool, what can make your urls look nice.
				The rewriting is controlled with regular expressions. To get more help
				look at http://www.urlrewriting.net/. 

				Remember to read the manual!

http://our.umbraco.org/wiki/recommendations/recommended-reading-for-web-developers

				The sample below rewrites a url from
				"/product/someproductid.aspx" to
				"/product.aspx?productid=someproductid" 

				The user will not see the rewritten path! The page that will be
				loaded from umbraco will instead be:
				"/product.aspx?productid=someproductid" 

				<add name="produktidrewrite"
					virtualUrl="^~/product/(.*).aspx"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/product.aspx?productid=$1"
					ignoreCase="true" />

				This sample is usefull for a productpage, where the product comes from a
				dynamic datasource, e.g. a database. The querystring "productid" can be loaded
				from the template, into a macro, that then loads the product!

				Any bugs or problems with the rewriter, contact Anders/Duckie
			-->
    <add name="Sector_SectorReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/sector/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/sectors.aspx?sectorReference=$2"
					ignoreCase="true" />
    <add name="Sector_SectorID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/sector/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/sectors.aspx?sectorID=$2"
					ignoreCase="true" />
    <add name="Companies_CommunityReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/companies/community/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/companies.aspx?communityReference=$2"
					ignoreCase="true" />
    <add name="Companies_SectorReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/companies/sector/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/companies.aspx?sectorReference=$2"
					ignoreCase="true" />
    <add name="Companies_BusinessKeyword_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/companies/([a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/companies.aspx?keyword=$2"
					ignoreCase="true" />
    <add name="Company_CompanyReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/company/([a-zA-Z0-9_]+)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/company.aspx?companyReference=$2"
					ignoreCase="true" />
    <add name="Company_CompanyID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/company/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/company.aspx?companyID=$2"
					ignoreCase="true" />
    <add name="Contact_SectorReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/contact/sector/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/contact.aspx?sectorReference=$2"
					ignoreCase="true" />
    <add name="Contact_CommunityReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/contact/community/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/contact.aspx?communityReference=$2"
					ignoreCase="true" />
    <add name="Contact_CompanyReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/contact/company/([a-zA-Z0-9_]+[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/contact.aspx?companyReference=$2"
					ignoreCase="true" />
    <add name="News_CompanyReferenceNewsID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/news/company/([a-zA-Z0-9_]+[^/])/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/news.aspx?companyReference=$2&amp;newsid=$3"
					ignoreCase="true" />
    <add name="News_CompanyReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/news/company/([a-zA-Z0-9_]+[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/news.aspx?companyReference=$2"
					ignoreCase="true" />
    <add name="News_CommunityReferenceNewsID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/news/community/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/news.aspx?communityReference=$2&amp;newsid=$3"
					ignoreCase="true" />
    <add name="News_CommunityReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/news/community/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/news.aspx?communityReference=$2"
					ignoreCase="true" />
    <add name="News_SectorReferenceNewsID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/news/sector/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/news.aspx?sectorReference=$2&amp;newsid=$3"
					ignoreCase="true" />
    <add name="News_SectorReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/news/sector/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/news.aspx?sectorReference=$2"
					ignoreCase="true" />
    <add name="News_NewsID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/news/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/news.aspx?newsID=$2"
					ignoreCase="true" />
    <add name="Products_CompanyReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/products/company/([a-zA-Z0-9_]+[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/products.aspx?companyReference=$2"
					ignoreCase="true" />
    <add name="Products_CommunityReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/products/community/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/products.aspx?communityReference=$2"
					ignoreCase="true" />
    <add name="Products_SectorReference_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/products/sector/([a-zA-Z]{1}[a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/products.aspx?sectorReference=$2"
					ignoreCase="true" />
    <add name="Products_ProductsKeyword_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/products/([a-zA-Z0-9_]*[^/])"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/products.aspx?keyword=$2"
					ignoreCase="true" />
    <add name="Product_CompanyReferenceProductID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/product/company/([a-zA-Z0-9_]+[^/])/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/product.aspx?companyReference=$2&amp;productID=$3"
					ignoreCase="true" />
    <add name="Product_CompanyIDProductID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/product/company/([1-9]+[0-9]*)/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/product.aspx?companyID=$2&amp;productID=$3"
					ignoreCase="true" />
    <add name="Event_CompanyReferenceEventID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/event/company/([a-zA-Z0-9_]+[^/])/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/event.aspx?companyReference=$2&amp;eventID=$3"
					ignoreCase="true" />
    <add name="Event_CommunityReferenceEventID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/event/community/([a-zA-Z0-9_]+[^/])/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/event.aspx?communityReference=$2&amp;eventID=$3"
					ignoreCase="true" />
    <add name="Event_EventID_Rewrite"
					virtualUrl="^~/([a-zA-Z]{1}[a-zA-Z0-9]*[^/])/event/([1-9]+[0-9]*)"
					rewriteUrlParameter="ExcludeFromClientQueryString"
					destinationUrl="~/$1/event.aspx?eventID=$2"
					ignoreCase="true" />
  </rewrites>
</urlrewritingnet>
<configuration>
	<urlrewritingnet configSource="ExternalRewrite.config"/>
...
</configuration>
References

For custom URL Rewriting modules refer to the following articles:

[1] http://www.codeproject.com/Articles/2538/URL-Rewriting-with-ASP-NET
[2] http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/
[3] http://msdn.microsoft.com/en-us/library/ms972974.aspx