6.01.2013

Extensionless URL Rewrite in ASP.NET

URL rewriting is on the most powerful and important feature one can implement when developing websites. Among many benefits the most obvious ones are security and easy to follow paths. Rewriting rules hide the querystring variables being passed and make it easier for search engines and visitors to understand and follow the pages.

A typical rewrite scenario is where you have a URL like http://www.site.com/Default.aspx?bookID=12. You can turn this URL into http://www.site.com/Books/12.aspx. You can even go one step further and remove the aspx extension, turning the URL into http://www.site.com/Books/12.

How to implement it

In this post I will use URLRewritingNet as an example. You can use other providers if you like, but the process is almost the same.

Step 1: The DLL
Download the rewrite dll from http://www.urlrewriting.net/159/en/downloads.html. It is a zip file which contains the actual dll (UrlRewritingNet.UrlRewriter.dll).

Step 2: DLL Installation
Add the UrlRewritingNet.UrlRewriter.dll file into the Bin folder of your website.


Step 3: Web Config Setup
Enter the following section into the configuration's <configsection> so that it can be advertised.

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


Step 4: Register rewrite as an HTTP Module
Since we will handle all incoming requests with UrlRewritingNet, we will have to register the component as Http Module in the <system.web /> section.

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


Step 5: Enter the URL Rewrite rules
After the end of <configsection> and inside the <configuration/> we will add the rewrite rules. It is these rules which are responsible for handling your URLs.

In the following example our rule will take the book id URL and translate it to the inner querystring url.

 <urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString"
defaultProvider="RegEx" defaultPage = "default.aspx"
xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
<rewrites >
<add name="Rule1" provider="RegEx"
virtualUrl="^~/Book/(.+)$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/Default.aspx?bookID=$1"
ignoreCase="true" />
</rewrites>
</urlrewritingnet>


Test run

If you visit /Book/12 it should translate to /Default.aspx?BookID=12

Troubleshooting

Rewrite rules with aspx extension work but the extension-less URLs give 404.
You may receive 404 (File or directory not found) error because under the Visual Studio webserver, every single request is passes through ASP.NET while in traditional IIS this is not the case. This can be solved by adding a UrlRewriteModule as a module in the <system.webServer> block.

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




Project files are available here: https://extensionlessurls.codeplex.com/SourceControl/latest