Go Back

URL Rewriting with Sitefinity's AdvancedUrlRewriter

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

Comments  4

  • Jon Payne 26 Jan

    Gabe - your basic URL post before this was a great help to me, thanks.  For this advanced method, can you re-write non-aspx file extensions?  That is, if your site has old .html file extensions that are now .aspx Sitefinity pages, can you redirect it from .html to .aspx or does this only work with .aspx to .aspx?  I've tried setting up ISAPI ReWrite to handle this but it won't work, as Sitefinity seems to force the .aspx file extension on to all requests.  Does this method precede that?  Thanks!!!

    And sorry for hitting you with a question a year after you posted it.  :)
  • Ansar 18 Feb

    Hi I have done exactly what is explained on this blog but it isn't work. I have added the following rule but it aint working:

    <

     

    urlrewrites>

     

    <!--

     

    Add your rule elements here -->

     

    <

     

    rule>

     

    <

     

    url>toto.aspx</url>

     

    <

     

    rewrite>titi.aspx</rewrite>

     

    </

     

    rule>

     

    </

     

    urlrewrites>

     


    Please help me!!!
  • ansar 18 Feb


    <urlrewrites>
          <!-- Add your rule elements here -->
          <rule>
            <url>toto.aspx</url>
            <rewrite>titi.aspx</rewrite>
          </rule>
        </urlrewrites>
  • Jerod 28 Mar

    From an SEO perspective, wouldn't this solution be bad? If 2 url's out there serve the same content, google penalizes you for having duplicate content. A better solution would be to do 301 redirects to the new url's.
Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: