Redirect with customizable redirect headers

The aliases provided by Sitecore provide the same basic functionality, but they will not get you very far if you want to be a bit more advanced and allow the administrators to select the type of redirect or expand the redirect functionality in some other way.

The solution is to create your own redirect functionality which is quite easily achieved by adding some templates and adding a pipeline to the httpRequestBegin processors.

Sitecore Templates

Lets start by adding the necessary templates. The first template is the redirect header template. Create it with only one single-line text field defining the header that we will send in the response status.

ResponseHeaderTemplate

Next create the redirect template. Set the source of the droplink field called ResponseStatus to point to the parent of the response headers that will be used in the solution.

RedirectTemplate

Create a structure for the redirects in /sitecore/content somewhere. And create the headers that the administrator needs. W3C has a definition of redirect headers.

RedirectHeaders

Next create a redirect item and select the 301 Moved Permanently header on it.

Redirect

The code

The basic work in Sitecore is done. Now to the code. Firstly create a class that will carry the information of the redirect.

    public class Redirect
    {
        public string OldUrl { get; set; }

        public string RedirectToUrl { get; set; }

        public string ResponseStatus { get; set; }
    }

Then create a repository that will return a list of all redirects in your solution. That should be pretty straight forward so I will not show it. But basically it populates a list of Redirect-objects where each redirect object has the field value of “UrlToRedirect” in the OldUrl property, the link url from the “RedirectToItem” field in the RedirectToUrl property and the value from the “ResponseStatus” field from the selected redirect header in the ResponseStatus property.

Now on to the actual redirecting. Create a class based on HttpRequestProcessor and call it RedirectResolver. Override the Process method.

public class RedirectResolver : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            Assert.ArgumentNotNull((object)args, "args");
            if (Context.Item != null || Context.Database == null || args.Url.ItemPath.Length == 0)
                return;

            List<Redirect> redirects = RedirectRepository.GetList();

            foreach (var redirect in redirects)
            {
                if (redirect.OldUrl.ToLower().Equals(HttpContext.Current.Request.Path.ToLower()))
                {
                    HttpContext.Current.Response.Status = redirect.ResponseStatus;
                    HttpContext.Current.Response.AddHeader("Location", redirect.RedirectToUrl);
                    HttpContext.Current.Response.End();
                }
            }
        }
    }

Configuration files

Now all there is left to do is to add the resolver to web.config. Add it to the httpRequestBegin section after the FileResolver so it looks something like this.

        <processor type="Sitecore.Pipelines.HttpRequest.AliasResolver, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.HttpRequest.DefaultResolver, Sitecore.Kernel" />
        <processor type="Sitecore.Pipelines.HttpRequest.FileResolver, Sitecore.Kernel" />
        <processor type="MyNamespace.RedirectResolver, MyAssembly" />

All done. Navigate to http://yoursite/test.aspx and you should be redirected to the home item.

Leave a Reply

Your email address will not be published. Required fields are marked *