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
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.
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.
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.
defaultProvider=”RegEx” defaultPage = “default.aspx“
<add name=”Rule1” provider=”RegEx“
If you visit /Book/12 it should translate to
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,
Project files are available here: https://extensionlessurls.codeplex.com/SourceControl/latest