Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nik 1599 posts 7179 karma points MVP 6x c-trib
    Feb 14, 2017 @ 15:43
    Nik
    1

    Custom 404 error page(s) Multi-Site

    Hey guys,

    So I need a hand, I'm running up against a brick wall and I think I'm going slightly mad.

    I have an Umbraco Instance with 2 sites in it (they aren't multi-lingual but are very closely related). What I'm trying to do is apply a styled, friendly, 404 error page in each site.

    So, lets start with the blogs I've used for pointers:

    1) https://blog.mortenbock.dk/2017/02/03/error-page-setup-in-umbraco/ This was a great article and very helpful, however a small note: I'm yet to create the fallback 404.html and 404.aspx files as I have been trying to get the content based 404's to work first.

    2) https://www.diplo.co.uk/blog/web-development/404-page-finder-for-multi-site-umbraco-installations/ AND http://24days.in/umbraco-cms/2014/the-double-album/multi-site-404/ These two basically say the same thing but are focused around using a content finder to get to 404 content page.

    So, my content finder class is a slight variation on the ones linked above. Instead of searching for a document type it grabs the home node and gets the value from a content picker which represents the 404 page.

    As far as I can tell however, this is working. I've put various logging steps in place and the log updates saying it finds the node which I've indicated is a 404. However, sometimes I get to see the content based 404 error, other times I just get the 404 error from the server.

    What I don't understand is why?

    The following is my resultant Content Finder:

    public class FourOhFourFinder : IContentFinder
    {
        #region Implementation of IContentFinder
    
        public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            LogHelper.Info(GetType(), string.Format("404 Finder triggered"));
            //Check request cannot be found
            if (contentRequest.PublishedContent == null)
            {
                var domain = contentRequest.UmbracoDomain;
    
                if (domain != null)
                {
                    LogHelper.Info(GetType(), string.Format("Domain Found - Root Node: {0}", domain.RootContentId.GetValueOrDefault(0)));
                    var home = domain.RootContentId.HasValue && domain.RootContentId.Value > 0 ?
                        contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(domain.RootContentId.Value) : null;
    
                    if (home != null)
                    {
                        //Get the 404 node
                        //The page not found is stored as a property on the home page.
                        var pageId = home.GetPropertyValue<int>("pageNotFound");
                        LogHelper.Info(GetType(), string.Format("404 page ID - {0}", pageId));
                        var notFoundNode = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(pageId);
                        LogHelper.Info(GetType(), string.Format("404 page node found ? - {0}", notFoundNode != null));
                        if (notFoundNode != null)
                        {
                            //Set Response Status to be HTTP 404
                            contentRequest.SetResponseStatus(404, "404 Page Not Found");
    
                            //Set the node to be the not found node
                            contentRequest.PublishedContent = notFoundNode;
                        }
                    }
                }
            }
    
            return contentRequest.PublishedContent != null;
        }
    
        #endregion
    }
    
    public class ContentFinderStartUp : ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, FourOhFourFinder>();
        }
    }
    

    Oh yeah, another thing. It all appears to work absolutely fine on my development environment. All of this strange behaviour only occurs on the Live environment. I did think cache related, but I've tried deleting the umbraco.config, re-publishing the site from the backoffice, recycling the app-pool. All to no resolution.

    Does anyone have any ideas of things I may have missed? If you need more information please let me know.

    Thanks,

    Nik

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Feb 15, 2017 @ 07:38
    Dave Woestenborghs
    102

    Hi Nik,

    Have you tried this package ? https://our.umbraco.org/projects/backoffice-extensions/umbraco-page-not-found-manager/

    This does all the heavy lifting for you.

    Maybe you can add some extra logging to check if the domain is found ? I think that is the issue here. That UmbracoDomain is not set and that's why it can't find the home node.

    Dave

  • Nik 1599 posts 7179 karma points MVP 6x c-trib
    Feb 15, 2017 @ 08:33
    Nik
    0

    Hi Dave,

    Thanks for the suggestion, I wasn't even aware of the package. I had no issues with finding the domain, it found the 404 node off of the home node and all of that was succeeding when looking at the logs I had in place. Just for some reason it wouldn't return the content.

    Having put that package in place it has allowed me to solve the issue which is great :-)

    Thanks very much.

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft