Now Anyone Can Create Redirect Pages in Sitecore

Making redirects is a typical need for any website, and they are very simple to implement as a Sitecore developer. Now, making them easy to implement for non-technical users can be easily done with this solution.

To give anyone the ability to add redirects, create a redirect “page” in Sitecore. In reality, this isn’t a page at all, although it is built the same way. Here’s how you developers can do it:

Steps for Creating a Redirect Page in Sitecore

1. Create a sublayout control to place in the main content control of our page. This is where the basic page sublayout control or community sublayout control would go:

2. Create the template that has the necessary fields so the marketing team can choose where the redirect page will redirect to. There is a built-in Sitecore General Link field type that is perfect for this. This field opens a copy of the Sitecore content tree so the user only has to pick the page that is the destination page of the redirect.

Along with the link field, add fields to disable pulling the source URL querystring parameters and a toggle for 302/301 redirect (Temporary/Permanent redirects). As an added precaution, you can set these fields with default values in the standard values of the template.

3. Change the template’s icon to an appropriate image and set the insert options on the home page to include the new Link Page Template for some nice bells and whistles, too.

4. Now, all that is needed is the driving code to handle the redirect. For this, get the Link Field object from Sitecore and determine if the link is an external link (to another website) or an internal link to another page in your site. Use the built-in TargetItem property which will either be an item in the site or null. At the same time, check for the querystring checkbox, and add/remove the querystring accordingly:

Item item = Sitecore.Context.Item;
LinkField field = (LinkField)item.Fields["DestinationURL"];
string url = "";
if (field.TargetItem != null)
     url = LinkManager.GetItemUrl(field.TargetItem);
     if (item.Fields["IncludeQueryStringParameters"].Value == "1")
          url += "?" + field.QueryString;
     url = field.Url;
     if (item.Fields["IncludeQuerystringParameters"].Value != "1" && url.IndexOf("?") >= 0)
          url = url.Substring(0, url.IndexOf("?"));

The next snippet handles the 302/301 redirect by setting the Location header of the HttpContext to the destination URL, then sets the status code and release:

HttpContext.Current.Response.AddHeader("Location", url);
if (item.Fields["IsPermanantRedirect"].Value == "1")
     HttpContext.Current.Response.StatusCode = 301;
     HttpContext.Current.Response.Status = "301 Moved Permanently";
     HttpContext.Current.Response.StatusCode = 302;


And that’s it!  Now, your marketing team or other non-technical users can create as many Sitecore redirects as they desire without the need of developer magic.  Who doesn’t love that?

« Prev Article
Next Article »