Tuesday, May 23, 2017

Sitecore 404 and 500 handle

Today we discuss, how we can handle 404 and 500 in Sitecore.
I found below URL useful as well.
https://www.akshaysura.com/2016/08/02/secure-sitecore-why-use-a-custom-500-error-page/ 
http://findgnosis.com/2015/05/14/ideal-error-handling-for-sitecore/ 

Handle 500 Error:
So first I talk about 500 error handle for that I use to replace below line in web.config

<customErrors mode="RemoteOnly"/>

to

<customErrors mode="On" defaultRedirect="~/Error/500Error.html" redirectMode="ResponseRewrite"/>

According to Akshay sura blog that is the good approach to take static HTML page for handle 500 error.

Handle 404 Error:

Now come to 404 error. For this I take two settings as below:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
        <settings>
      <!--For handle 404 error-->
      <setting name=CustomError.Page404Resolver" value="ItemId"/>
      <setting name="RequestErrors.UseServerSideRedirect" value="true" />
    </settings>
  </sitecore>
</configuration>

The first one takes the item id for the 404-page item that is shown when 404 error occurs.
Second setting just keeps maintaining the same address which we request for. If you don't use this setting then after resolving item it shows 404 item name in URL instead of address which you type.

For handle 404 we place our code after item resolve that makes significant action if item not found(404).

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor type="CustomError.ErrorHandler.Page404Resolver, CustomError"

                   patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"/>
      </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration>

Page404Resolver.cs:


namespace CustomError.ErrorHandler
{
    public class Page404Resolver : HttpRequestProcessor
    {
        public override void Process(Sitecore.Pipelines.HttpRequest.HttpRequestArgs args)
        {
            if (Sitecore.Context.Item != null || Sitecore.Context.Site == null || Sitecore.Context.Database == null || WebUtil.GetRawUrl().IndexOf("/sitecore") > -1)
            {
                if (Sitecore.Context.Database != null && ID.IsNullOrEmpty(LanguageManager.GetLanguageItemId(Sitecore.Context.Language, Sitecore.Context.Database)))
                {
                    SetCustomErrorPage(args);
                }
                else
                {
                    return;
                }
            }
            SetCustomErrorPage(args);
        }

        private void SetCustomErrorPage(HttpRequestArgs args)
        {
            string errorPagePath = Settings.GetSetting("CustomError.Page404Resolver");

            var item = args.GetItem(errorPagePath);
            if (Sitecore.Context.Item != null && Sitecore.Context.Item.Versions.Count == 0)
            {
                item = item.Database.GetItem(item.ID, Sitecore.Globalization.Language.Parse("en"));
                Sitecore.Context.Language = Sitecore.Globalization.Language.Parse("en");
            }
            if (item != null)
            {
                Sitecore.Context.Item = item;
                args.Context.Response.StatusCode = 200;
                args.Context.Response.TrySkipIisCustomErrors = true;
            }
        }

    }
}


In case if item not found(sitecore context item have null value) on that case above code get the 404 page item id from setting variable and resolve the item of 404 and replace with context item.

Hope that may help you to resolve 404 and 500 error.
Till that happy Sitecoring :)

Please leave your comments or share this article if it’s useful for you.

1 comment: