Copied to clipboard

Flag this post as spam?

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


  • Dan 1285 posts 3917 karma points c-trib
    Mar 12, 2024 @ 11:50
    Dan
    0

    'Catch-all' redirects on not-found content

    Hi,

    We're using SkyBrud Redirects to manage specific 301 redirects on a new site. However, there are a couple of paths which have lots of legacy content with no new equivalent pages, so we'd like to implement some sort of catch-all redirect on these paths specifically when they would otherwise result in a 404. e.g.

    "If the requested path begins /news/ and there are no redirects already configured for this and there is no content found on the requested path, redirect to /news/."

    There are some specific URLs beginning /news/ which have new content, so those should work as normal. There are also some specific URLs with 301 redirects set up, and those too should continue to redirect. It's the 'others' that we would like to identify and redirect.

    It looks like a ContentFinder is the way to go, but I'm not sure as to the best way of identifying whether the current request is not found into the logic:

    public class MyContentFinder : IContentFinder
    {
        private readonly IUmbracoContextAccessor _umbracoContextAccessor;
    
        public MyContentFinder(IUmbracoContextAccessor umbracoContextAccessor)
        {
            _umbracoContextAccessor = umbracoContextAccessor;
        }
    
        public Task<bool> TryFindContent(IPublishedRequestBuilder contentRequest)
        {
            var path = contentRequest.Uri.GetAbsolutePathDecoded();
            if (path.StartsWith("/news/")) // need to check if current request does not exist
            {
                contentRequest.SetRedirectPermanent("/news/");
                return Task.FromResult(true);
            }
    
            if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
            {
                return Task.FromResult(false);
            }
    
            return Task.FromResult(false);
        }
    }
    

    (I'm also not sure whether the default return if the conditions are unmet should be Task.FromResult(false)?)

    Could anyone suggest the best way to get this working, assuming we're going in the right direction already?

    Many thanks.

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Mar 12, 2024 @ 21:48
    Marc Goodson
    100

    Hi Dan

    Yes the Content Finder IS the way to go, as you want your redirect to happen for items that aren't new to that folder.

    So you want Umbraco to 'have a go first' and if it doesn't match a new article, then you want your custom logic to take hold.

    Content Finders are executed in a specified order... I use to use this drawing to help explain this during training...

    http://tooorangey.co.uk/media/1146/contentpipeline.jpg

    You can see the core order of content finders here...

    https://github.com/umbraco/Umbraco-CMS/blob/d8b0616434d1114d0628fb5702206bea39baa88b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Collections.cs#L57

    So as long as you register your Content Finder after Umbraco's ContentFinderbyUrl and before ContentFinderByRedirectUrl (this is how the redirect url manager dashboard works)

    Then you should only be actioning your custom logic for the old pages...

    In terms of what you return from a content finder, if you return false you are saying 'hey I can't match this' and it allows umbraco to move on to the next Content Finder in the queue to let that have a chance of finding content...

    When you return true, you are telling Umbraco, you have found the content and there is no need for any other content finders to try.

    One other thing if you are redirecting for SEO reasons its important to redirect to equivalent content, if you redirect everything to the same generic page, you could get penalised for serving a 'soft 404'... If the news article has genuinely gone the serving a 310 status rather than a redirect may be more appropriate... Anyway the main thing is you are 99% there with the Content Finder.

    Regards

    Marc

  • Dan 1285 posts 3917 karma points c-trib
    Mar 13, 2024 @ 10:51
    Dan
    0

    I appreciate a crow in a technical diagram almost as much as a beautiful, generous and helpful forum reply, so thanks for both, Marc! :)

Please Sign in or register to post replies

Write your reply to:

Draft