In my previous blog post I wrote about the importance of having clean URLs. I also wrote about the easy to use URL management features in Sitefinity. In this post we'll explore the AdvancedUrlRewriter class included in Sitefinity's API. This class applies rewriting rules to all incoming URLs.
If your web site contains hundreds of legacy URLs, it would be time consuming to manually add these URLs inside Sitefinity. Using AdvancedUrlRewriter you can quickly handle these legacy URLs.
What does AdvancedUrlRewriter do?
Sitefinity's AdvancedUrlRewriter looks at each incoming URL and allows you to change it (if you wish) based on rules you create. These rules are based on .NET regular expressions.
Here is an example:
Incoming URL:
http://www.examplewebsite.com/view-product.aspx?name=sitefinity
Rewriting Rule:
<rule>
<url>/view-product\.aspx?name=(\s+)</url>
<rewrite>/product/$1.aspx</rewrite>
</rule>
Rewritten URL:
http://www.examplewebsite.com/product/sitefinity.aspx
Using this feature you can handle URL's that do not exist and map those URLs to valid pages.
Note: The URL rewriting is done server-side, not client-side. The URL originally requested will continue to display in the browser.
Enabling the AdvancedUrlRewriter
To use AdvancedUrlRewriter you need to first enable it. This is done in your ~/web.config file.
1. Add the following urlrewrites <section> to your <configSections>.
<configSections>
<sectionGroup name="telerik">
...
<section name="urlrewrites" type="Telerik.Cms.Web.AdvancedUrlRewriter, Telerik.Cms"/>
...
</sectionGroup>
</configSections>
2. Replace the default CmsHttpModule httpModule with CmsHttpModuleUrlRewrite.
<httpModules>
...
<add name="Cms" type="Telerik.Cms.Web.CmsHttpModuleUrlRewrite, Telerik.Cms"/>
...
</httpModules>
Making these 2 changes to your ~/web.config file will enable AdvancedUrlRewriter for your Sitefinity web site.
Adding URL Rewriting Rules
The rules AdvancedUrlRewriter applies to incoming URLs must be added to the ~/web.config file.
Add the following <urlrewrites> section to the the <telerik> section.
<telerik>
...
<urlrewrites>
<!-- Add your rule elements here -->
<rule>
<url>[input URL pattern]</url>
<rewrite>[replaced URL]</rewrite>
</rule>
</urlrewrites>
...
</telerik>
URL rewriting rules are simply .NET regular expressions. Anything you can do with a regular expression, you can also do with your URL rewriting rules.
Below are some example rules:
<urlrewrites>
<rule>
<!-- http://wwww.examplewebsite.com/OldLink.aspx -->
<url>OldLink\.aspx</url>
<!-- http://wwww.examplewebsite.com/NewLink.aspx -->
<rewrite>NewLink.aspx</rewrite>
</rule>
<rule>
<!-- http://wwww.examplewebsite.com/09-01-08/Website-Launches.aspx -->
<url>/(\d\d\-\d\d\-\d\d)/</url>
<!-- http://wwww.examplewebsite.com/news/09-01-08/Website-Launches.aspx -->
<rewrite>/news/$1/</rewrite>
</rule>
<rule>
<!-- http://wwww.examplewebsite.com/view_product.aspx?name=sitefinity -->
<url>/view-product\.aspx?name=(\s+)</url>
<!-- http://wwww.examplewebsite.com/product/sitefinity.aspx -->
<rewrite>/product/$1.aspx</rewrite>
</rule>
</urlrewrites>
The most difficult part of creating these URL rewriting rules will be creating the regular expressions. If you're unfamiliar with .NET regular expressions, check out some of the Getting Started Guides found on the Internet.
Additional Resources
There is an entire section devoted to URL rewriting in the Sitefinity User Manual:
http://www.sitefinity.com/documents/UserManual_3_5.pdf
Forum thread describing how to setup:
http://www.sitefinity.com/support/forums/support-forum-thread/b1043S-tabbb.aspx
How to setup if using IIS 7 with integrated pipeline mode:
http://www.sitefinity.com/support/forums/support-forum-thread/b1043S-btkatb.aspx
More information on integrated pipeline mode:
http://www.code-magazine.com/Article.aspx?quickid=060103
.NET Regular Expressions:
http://msdn.microsoft.com/en-us/library/hs600312(VS.71).aspx