A Redirect Template for Sitecore

Let's say you want to build an MVC navigation component for your Sitecore website, and you have these requirements:

  • The navigation hierarchy should be dynamic, and should derive from the content tree
  • The navigation must support drop-down lists of child items
  • Some items in the content tree will only be placeholders and will not have content
  • If you navigate to the URL of a placeholder item, you should be redirected to a valid Sitecore content item, or external web page

If, for example, your content tree has the following structure and business requirements:

- About Us (Placeholder Item - if you navigate to this URL, redirect to the Overview page)
    - Overview
    - Our History
    - Our Leadership
    - Office Locations
- Services
- Industries
- Insights
- Press
- Careers (Placeholder Item - if you navigate to this URL, redirect to the external careers website)
- Investor Relations (Placeholder Item - if you navigate to this URL, redirect to the external investor's website)
- Contact Us

So your resulting navigation component might look something like this:

So now you have to figure out what to do if someone navigates to one of your placeholder items, About Us, Careers or Investor Relations. This is where the concept of a Redirect Template comes in to play! A redirect template is a content item that you can configure to redirect to another internal or external URL.

Follow these instructions to build your own redirect template.

1. Create a new Template in Sitecore, and name it "Redirect". Create a new section named "Redirect", and add a General Link field to it named "Redirect To". Customize the other properties of the template as needed, such as icon, help text, fallback properties and standard values.

2. Create a controller class in your Visual Studio solution.

using Glass.Mapper.Sc.Web.Mvc;
using Sitecore.Data.Fields;
using Sitecore.Links;
using Sitecore.Mvc.Presentation;
using System.Web.Mvc;

namespace Integryx.Feature.Redirect.Web.Controllers
    public class RedirectController : GlassController
        const string REDIRECT_TO_ID = "{19BBB4A8-A09C-4CB3-A686-C26DEDA0434F}";

        public override ActionResult Index()
            var link = (LinkField)RenderingContext.Current.ContextItem.Fields[REDIRECT_TO_ID];
            if (link != null)
                if (link.IsInternal)
                    return Redirect(LinkManager.GetItemUrl(link.TargetItem));
                    return Redirect(link.Url);

            return View();

As you can see from the code, the purpose of this controller, which will be called anytime someone navigates to one of your Redirect items, is to check to see if the "Redirect To" field has been configured. If it has, then determine if the target URL is internal or external, then redirect to the new URL. 

I'm using GlassMapper, but you don't have to. Change the inherited class from GlassController to Controller if you need to.

Note the REDIRECT_TO_ID constant. You'll need to update this to the ID of the "Redirect To" field you created in your Sitecore template.

3. Now you can create a Controller Rendering. Name it "Redirect Rendering", and give it the following properties:

4. And finally, you are ready to build out your content tree using your new Redirect Rendering for the About Us, Careers and Investor Relations items:

  • For the About Us item, set the Redirect To field to point to the Overview item.
  • For the Careers item, set the Redirect To field to point to the external careers website.
  • For the Investor Relations item, set the Redirect To field to point to the external investor's website.

Build and publish, and your new Redirect Template is ready to use!

As always, there are at least a half-dozen other ways to do this kind of thing (including IIS-level redirects and URL rewrites), but I find this method to be quite useful as it allows content editors to configure page-level redirects without needing a developer.

Please let me know your thoughts on Redirect Templates, and any way I could improve my code or this blog.

Happy redirecting!


Add comment