Sitecore Multi-Site custom 500 error page

This post is an add on to my previous post Secure Sitecore : Why use a custom 500 error page? In this post I will go through how you can get a multi-site custom 500 error page in a Sitecore solution.

This would mean that each site in your Sitecore instance can have it’s individualized custom 500 error page. As I mentioned before, I truly believe it makes absolute sense to have a static 500 error page rather than on inside of Sitecore for obvious reasons.

A version of this code exists in production environments and has been working like a charm.

First, we need to add Global.asax because we need to process Application_Error event. Second, try not to use any Sitecore API in this file.

We added an AppSettings config flag to control the processing. In your web.config add the following to the AppSettings section:


You would also need to make a few changes in the Web.config. First add in the httpErrors node in :


Also set the customErrors mode as On.

Next we need to add the Application_Error event in the global.asax.cs file. Here is a sample below:

using System;
using System.Configuration;
using System.Web;

namespace YOURNAMESPACE
{
    public class Global : Sitecore.Web.Application
    {

        protected void Application_Error(object sender, EventArgs e)
        {
            var lastException = Server.GetLastError();

            bool show500page = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["Show500PagePerSite"]) && ConfigurationManager.AppSettings["Show500PagePerSite"].ToLower().Equals("true") ? true : false;

            if (show500page)
            {
                HttpContext.Current.Server.ClearError();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.TrySkipIisCustomErrors = true;
                HttpContext.Current.Response.StatusCode = 500;
                HttpContext.Current.Response.StatusDescription = "500 Internal Server Error";
                Server.Transfer(GetSitePath(HttpContext.Current.Request.Url.GetComponents(UriComponents.Host, UriFormat.Unescaped))); // set the redirect location
            }
        }

        private string GetSitePath(string hostname)
        {
            if (hostname.Contains("FIRSTSITEHOST")) return "/errors/FIRSTSITEERRORPAGENAME.html";
            if (hostname.Contains("SECONDSITEHOST")) return "/errors/SECONDSITEERRORPAGENAME.html";

            return "/";
        }
    }
}

Hope this helps you out with your solution.

If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).

1 thought on “Sitecore Multi-Site custom 500 error page”

  1. Hi Akshay,

    I heard Application_Error event (in global.asax) no more hitting from sitecore 8.2 onward and same i am also facing the issue .

    Few posts : https://sitecore.stackexchange.com/questions/4773/handle-all-custom-errors-in-sitecore-mvc
    http://www.partech.nl/nl/blog/2014/01/sitecore-mvc-applications-and-the-application-error-event

    I have tried patching but still custom class not hitting and i can see yellow error page.Application_Error event working till the 8.1 .
    Any solution do you have ?

    Thanks in advance !!
    PS

Leave a Reply

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