Tuesday, December 11, 2007

URL Rewriting in ASP.NET with No Code

http://urlrewriter.net/ provides a great little tool for URL rewriting in ASP.NET. I used this on a site where SEO was of particular concern. The best part was that it required no code.

Instead of mydomain.com/reader/Default.aspx?id=12345&type=book I could enable links like mydomain.com/reader/Default.aspx/12345/book.

Here's how I enabled this using Web.config:
<configuration>
<!--...-->
<configSections>
<!--...-->
<!-- For URLRewriter -->
<section
name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.
RewriterConfigurationSectionHandler,
Intelligencia.UrlRewriter" />
</configSections>

<system.web>
<!--...-->
<httpModules>
<add
name="UrlRewriter"
type="Intelligencia.UrlRewriter.RewriterHttpModule,
Intelligencia.UrlRewriter"/>
</httpModules>
</system.web>

<rewriter>
<rewrite
url="~/reader/Default.aspx/(.*)/(.*)/$"
to="~/reader/Default.aspx?id=$1&type=$2" />
<rewrite
url="~/reader/Default.aspx/(.*)/(.*)$"
to="~/reader/Default.aspx?id=$1&type=$2" />
</rewriter>

</configuration>

No comments: