There are many ways to implement this functionality in Sitecore. I have mentioned the below two ways

  • Using default configuration and assigning custom page/ url to specific value

Go to SitecoreSettings.config and where you can find settings related to ‘not found’

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>
      <!--  REQUIRE LOCK BEFORE EDITING
            If true, the user must have a lock on a document before
            he can edit it, otherwise it is always ready for editing
      -->
      <setting name="RequireLockBeforeEditing" value="false"/>
      
    </settings>
  </sitecore>
</configuration>

Create a path under APP_Config and override above settings

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>
      <setting name="ItemNotFoundUrl">
        <patch:attribute name="value">/home/404.html</patch:attribute>
      </setting>
      <setting name="LinkItemNotFoundUrl">
        <patch:attribute name="value">/home/404.html</patch:attribute>
      </setting>
      <setting name="LayoutNotFoundUrl">
        <patch:attribute name="value">/home/404.html</patch:attribute>
      </setting>
       <setting name="ErrorPage">
        <patch:attribute name="value">/home/Error.html</patch:attribute>
      </setting>
    </settings>
  </sitecore>
</configuration>

Go To web.config and modify below values
<customErrors mode="On" />
<customErrors mode="RemoteOnly" defaultRedirect="/ErrorPage.htm"/>

  • Overriding default classes by creating custom pipelinePatch:
    <processor type="Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel">
              <patch:attribute name="type">Sitecore.Demo.ErrorHandlingPipeline, customnamespace</patch:attribute>
            </processor>
    
    Pipeline:

     

    namespace Sitecore.Demo
    {
    
        /// <summary>
        /// This Pipeline Component overrides the default redirection logic for Item Not Found, Layout Not Found
        /// specified in the Multi Site Configuration...
        /// </summary>
        public class ErrorHandlingPipeline : Sitecore.Pipelines.HttpRequest.ExecuteRequest
        {
    
            protected override void RedirectOnItemNotFound(string url)
            {
                Sitecore.Diagnostics.Log.Warn(
                    this + " : No item for " + HttpContext.Current.Request.RawUrl + " (" + Sitecore.Context.User.Name + ")",
                    this);
    
                var siteConfig = Sitecore.Context.Site.GetMultiSiteContext();
                if (siteConfig != null)
                {
                    if (!string.IsNullOrEmpty(siteConfig.ItemNotFoundPage))
                    {
                        var context = System.Web.HttpContext.Current;
    
                        try
                        {
    
                            // NotFound page html content
                            string domain = context.Request.Url.GetComponents(UriComponents.Scheme | UriComponents.Host, UriFormat.Unescaped);
                            string content = Sitecore.Web.WebUtil.ExecuteWebPage(string.Concat(domain, siteConfig.ItemNotFoundPage));
    
                            // set Response params
                            context.Response.TrySkipIisCustomErrors = true;
                            context.Response.StatusCode = 404;
                            context.Response.StatusDescription = "Page not found";
    
                            // write out NotFound page html content
                            context.Response.Write(content);
                            //context.Response.End();
    
                            return;
                        }
                        catch (Exception ex)
                        {
                            //Web Client failed, maybe auth issue, do normal redirect.
                            this.PerformRedirect(siteConfig.ItemNotFoundPage);
                            return;
                        }
                    }
                    
                }
                base.RedirectOnItemNotFound(url);
            }
    
            protected override void RedirectOnLayoutNotFound(string url)
            {
                Sitecore.Diagnostics.Log.Warn(
                    this + " : No layout for " + HttpContext.Current.Request.RawUrl + " (" + Sitecore.Context.User + " - " + Sitecore.Context.Device.Name + ")",
                    this);
    
                var siteConfig = Sitecore.Context.Site.GetMultiSiteContext();
                if (siteConfig != null)
                {
                    if (!string.IsNullOrEmpty(siteConfig.LayoutNotFoundPage))
                    {
                        this.PerformRedirect(siteConfig.LayoutNotFoundPage);
                        return;
                    }
                    if (!string.IsNullOrEmpty(siteConfig.ItemNotFoundPage))
                    {
                        this.PerformRedirect(siteConfig.ItemNotFoundPage);
                        return;
                    }
                }
                base.RedirectOnLayoutNotFound(url);
            }
        }
    }


Leave a Reply

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

Related Posts

Fake DB

How to do unit testing for media items in Sitecore + xUnit testing : Fake DB

Unit testing in Sitecore is a tricky one since you need create an items in Fake dB. I didn’t find much information about Media items unit testing and below piece of code may help you! Read more…

Sitecore

how to enable full rich text editor in sitecore

Add below paths in datasource location /sitecore/system/Settings/Html Editor Profiles/Rich Text Default /sitecore/system/Settings/Html Editor Profiles/Rich Text Full /sitecore/system/Settings/Html Editor Profiles/Rich Text IDE /sitecore/system/Settings/Html Editor Profiles/Rich Text Medium

Sitecore

Sitecore 8 Experience Editor: A serious error has occurred please contact the administrator

In Sitecore version 8.0 rev. 150621 I am experiencing issue when clicking on Experience Editor or Preview button in the Publish tab. The experience editor opens with the “A serious error has occurred please contact Read more…