Copied to clipboard

Flag this post as spam?

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


  • Juris 2 posts 72 karma points
    Jul 13, 2023 @ 09:58
    Juris
    0

    Redirect to 410 error page if content is unpublished.

    Hi!

    I wanted to redirect users to 410 error page if content is being unpublished. Currently I am getting redirected to 404 page. What is the best way to achieve this?

    Thanks in advance! )

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Jul 16, 2023 @ 11:10
    Marc Goodson
    0

    Hi Juris

    There used to be a package called UrlTracker that used to do this.

    But it's possible to do it by extending Umbraco.

    Essentially the issue is, when a request for a url comes into Umbraco tries to find the matching content item from the published cache, following routing rules defined in c# classes that implement a special Umbraco interface IContentFinder.

    A Url to a Content item can be completely customised, so the actual Url for a page isn't stored anywhere in the database, it's only defined by the IContentFinder rules.

    When you unpublish a page, it's removed from the published cache, and so Umbraco doesn't know the difference between a subsequent request for it and a request for a page that doesn't exist - eg there is nothing recording 'what the Url was that it was published under'

    So in order to implement something, you need to handle the Unpublishing/Trashing Event defined in the ContentService

    https://our.umbraco.com/Documentation/Reference/Events/ContentService-Events/

    These fire just before the item is unpublished or deleted, and you can therefore find what is the 'current url' the item is published on.

    Then you need to store that somewhere, you can either create a Custom Database table, and add the Url to it (or I've seen sites where there is a label property on the Umbraco Content Item, called LegacyUrl, and you use the ContentService to programmatically populate this with the value of the Url it was published under, but this only works if the items aren't deleted!)

    Then you need to create a custom IContentFinder https://our.umbraco.com/Documentation/Reference/Routing/Request-Pipeline/IContentFinder

    and add it in the queue at the end, so it only fires if all the other ContentFinder rules have failed to find content for the Url, and before the 404 is served.

    Then your ContentFinder, should use the Url of the request to either lookup in the database or use an Examine search on the InternalIndex (which includes unpublished content) to find the former Url.

    If it finds the old url, then it can set the status of the request to be 410 and stop further ContentFinders from being requested by returning true, so no fall through to the 404 page occurs.

    Or at least that's the gist of it!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft