ModRewrite For .Net Applications (prior to iis7)
One thing web developers from linux backgrounds really miss in windows is using modrewrite to rewrite urls. In dot net, this is actually a tad trickier, especially with the new asp 2.0.
One solution is to use a DLL produced by ThunderMain, called URLRewriter. Because this solution requires the Dot net framework to process the rewriting of URLs, a wildcard has to be setup within IIS to allow all extensions to be processed by the dot net framework. The disadvantage over this is legitimate requests for files, such as images or html documents, will be routed through your application and hence exceptions will have to be made.
Once URLRewriter has been setup correctly for your application, you may notice that form post backs actually get posted back to the original and not the rewritten location. This is only ugliness for 1.0-1.1, but in 2.0+ it has a more terminal impact of causing the page’s viewstate on postback to not match that of the original rewritten url, resulting in a viewstate error. Changing the action of the form can be a right pain, as dot net really doesn’t take kindly in altering things behind its back. To correct this problem, there’s a nice DLL I have written to change the form action to whatever the current rewritten URL is. (legacy - currently unavailable)
Installing URLRewriter
Download the dll, and add a reference within your project. Within global.asax.vb we need to call the rewriter at the beginning of each request
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fire the URL Rewriter Fires at the beginning of each request
ThunderMain.URLRewriter.Rewriter.Process()
End Sub
Add the following code into the relevant location within the Web.Config
<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter, ThunderMain.URLRewriter, Version=1.0.783.30976, Culture=neutral, PublicKeyToken=7a95f6f4820c8dc3"/>
</sectionGroup>
</configSections>
</configuration>
The rewrite rules are set within the Web.Config, as shown. They are formatted as standard regular expressions, as modrewrite is.
<system.web>
<urlrewrites>
<!-- switches .html to .aspx -->
<rule>
<url>^(.*)\.html$</url>
<rewrite>$1.aspx </rewrite>
</rule>
<!-- Rewrites “Geeky/12-ModRewrite.aspx” into Default.aspx?ID=12&Category=Geeky&PageTitle=ModRewrite -->
<rule>
<url>(.*)/([0-9]+)-(.*)\.aspx</url>
<rewrite>Default.aspx?ID=$2&Category=$1&PageTitle=$3</rewrite>
</rule>
</urlrewrites>
</system.web>
From the above rules, you can see the benefit of rewriting query strings to look like directories and physical files. Not only does it make it easier for people to type the address into their browser, but it also is a help in getting large sites indexed by search engines. If your URL has a large amount of query strings, search engines may not index the whole range within the site, however, by rewriting the URLs, it appears that each of the pages in fact originate from individual files.
