Go Back

URL Rewriting Webinar Notes

A quick look at various techniques for managing URLs in Sitefinity.

Using the Basic Built-In URL Management Tools

  • Adding additional URLs to pages through Sitefinity page editor.
  • Adding external redirect links through Sitefinity page editor.

Using urlRewriteFormat for existing modules

urlRewriteFormat parameters are found in the web.config file and applied to Generic Content-based modules.

Example: urlRewriteFormat="~/{Provider}/{LibraryName}/[Name].sflb.ashx"

[] - indicates meta field. 
{} - indicates keyword that has hard-coded meaning for GC module.

Potential hard-coded items that can be used:

{ID} - hash code for content item (note the ID itself is not guaranteed to
be unique, it is used in case URL format generates duplicate URLs then this
ID is appended to the URL but you can also set it explicitly)
{GUID} - the actual (unique) ID of the content item
{Provider} - specifies data provider name
Library module overrides this method and adds additional keyword
{LibraryName} - specifies the name of the library

Refer to <metaFields> section in the web.config for reference on available meta fields.

To update old items touch each of them with this code:

protected void Page_Load(object sender, EventArgs e)
{
    Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager("News");  
    IList news = contentManager.GetContent();
    foreach (Telerik.Cms.Engine.IContent newsItem in news)  
    {
        string title = newsItem.GetMetaData("Title").ToString();
        Response.Write(" - " + title + "<br/>");

        Telerik.Cms.Engine.IContent updateItem = contentManager.GetContent(newsItem.ID);
        updateItem.SetMetaData("Title", title);
        contentManager.SaveContent(updateItem);
    }  
}

More information on updating existing items can be found here:
http://www.sitefinity.com/help/developer-manual/content-items-update.html
http://www.sitefinity.com/support/forums/support-forum-thread/b1043S-bddgkt.aspx

Using AdvancedUrlRewriter

1. Add a configuration section element (<configSections>):

<configSections>
 <sectionGroup name="telerik">
   ...
   <section name="urlrewrites" type="Telerik.Cms.Web.AdvancedUrlRewriter, Telerik.Cms"/>
   ...
 </sectionGroup>
</configSections>

2. Replace the default CmsHttpModule with CmsHttpModuleUrlRewrite:

<httpModules>
  <add name="Cms" type="Telerik.Cms.Web.CmsHttpModuleUrlRewrite, Telerik.Cms"/>
</httpModules>
3. Add a <urlrewrites> section:
<telerik>
  ..
  <urlrewrites>
    <!-- Add your rule elements here -->
    <rule>
      <url>[input URL pattern]</url>
      <rewrite>[replaced URL]</rewrite>
    </rule>
  </urlrewrites>
  ..
</telerik>

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

Example Rewriting Rules:

<urlrewrites>
  <rule>
    <url>Getting_Started_with_Open_Access\.aspx</url>
    <rewrite>Getting_Started_with_OpenAccess.aspx</rewrite>
  </rule>
  <rule>
    <url>/urlrewriting\-done/(\d\d\-\d\d\-\d\d)</url>
    <rewrite>/urlrewriting-done/news/$1</rewrite>
  </rule>
</urlrewrites>

Using a Custom HttpModule

Information about Permanent HTTP 301 Redirects:
http://www.webconfs.com/how-to-redirect-a-webpage.php

Forum thread on this subject:
http://www.sitefinity.com/support/forums/support-forum-thread/b1043S-bkeaah.aspx

Create ~/App_Code/PermanentRedirectHttpModule.cs

using System;
using System.Web;

namespace Custom
{
    public class PermanentRedirectHttpModule : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            HttpRequest request = context.Request;

            if (request.Url.PathAndQuery.Contains("Getting_Started_with_Open_Access.aspx"))
            {
                string url = request.Url.ToString();
                url = url.Replace("Open_Access.aspx", "OpenAccess.aspx");

                context.Response.Status="301 Moved Permanently";
                context.Response.AddHeader("Location", url);
                context.Response.End();
            }
        }
    }
}

Add this module to the <httpModules> section in ~/web.config:

<httpModules>  
 ...  
 <add name="PermanentRedirectHttpModule" type="Custom.PermanentRedirectHttpModule, App_Code" />
 ...
</httpModules>  

Using the ReplaceTool

Download tool at:
http://www.sitefinitywatch.com/Libraries/Downloads/ReplaceTool.sflb.ashx

BE SURE TO BACKUP YOUR DATABASE BEFORE EVEN LOOKING AT THIS

1. Extract [Archive]\App_Code\ReplaceTool.cs file to ~/App_Code directory
2. Extract [Archive]\Tools directory to your ~/Tools directory. If you do not have Tools directory, please create it.
3. Open your web.config file, and add the highlighted line to the <tools> node:

<tools>  
 ...  
 <add type="Telerik.Cms.Tools.ReplaceTool, App_Code" />  
 ...
</tools>  

Login to the Sitefinity Admin and navigate to Administration -> Tools -> ReplaceTool.

Random Stuff

Visual Studio theme:
http://www.agileprogrammer.com/dotnetguy/archive/2006/09/07/19030.aspx

Comments  6

  • Richard Baugh 15 Dec

    One thing I found was that there is a RegEx pattern that is used and any matches are replaced by the urlWhitespaceChar attribute value. By default this is hard-coded but can be overwritten by adding the urlReplaceSymbolsRegEx attribute. We discovered this when we were trying to get our blog old links to match up. The old links used a slash, "/", to delimit the Publication_Date. We replace the urlDateTimeFormat attribute with a pattern that used the slashes, but only to find out that they would get replaced by the urlReplaceSymbolsRegEx pattern.
  • JCriscione 08 Feb

    I tried using your URL Rewrite above, and can get it to work for replacing parts of the URL in most cases. If i have a URL that is http://domain.com/directory/ and try to remap it to http://domain.com/directory/home.aspx it doesn't work. Any suggestions?
  • Tys 11 Nov

    Hi Gabe,
    I've watched your tutorial and learned a lot, but i'm still stuck on one thing.. Redirecting from different levels of sub-folders within a site, to one specific .aspx file in the root of my site. I think i need a workaround for the missing ~/implementation. Do you have any idea? 
    Greets, Tys 

  • TruMan1 21 Jan

    GOOD question! I'd also like to know how to rewrite folders to .aspx pages. For example, rewriting http://example.com/page1 to http://example.com/page.aspx? By the way, what are the implications of rewriting like this in Sitefinity?
  • Christopher Rannow 23 Feb

    Is it possible to do extensionless URL rewriting? Similar to URLRewritingNet? Or one better is it possible to use URLRewritingNet with Sitefinity?
  • Mickey 20 Apr

    AKAIK you've got the aswner in one!
Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: