Copied to clipboard

Flag this post as spam?

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


  • Jon 92 posts 166 karma points
    May 13, 2022 @ 15:04
    Jon
    0

    Create a Redirect when a page is unpublished

    We have a section where events are shown in a list. An email is sent out to users.

    BUT if an event has ended the page is Un-Published. So when the user clicks on their email link it takes them to a 404 page.

    Is there a way where the user could be taken to a different type of page - such as a friendly page explaining that the event has ended and not a 404 page?

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    May 13, 2022 @ 18:23
    Paul Seal
    0

    Hi Jon

    This sounds like an ideal candidate for using the Skybrud Redirects package. Just before you unpublish the page you add an entry to Skybrud redirects and point it to the event ended page. You could even go further and hook into the unpublish event and write it into the Skybrud Redirects table automatically.

    https://our.umbraco.com/packages/website-utilities/skybrud-redirects/

    Kind regards

    Paul

  • Jon 92 posts 166 karma points
    May 13, 2022 @ 18:43
    Jon
    0

    Thank you so much. Is it possible to add a redirect into the redirects table via the Skybrud code? I have had a look but couldn't find anything but probably looking in the wrong place.

    Do you have any example code?

    Thanks again

  • Philip 1 post 71 karma points
    Jul 26, 2022 @ 09:42
    Philip
    0

    Hi Jon,

    I did this using nPoco.

    First I made a model matching all the columns in the table created by the Skybrud package.

    using System;
    using System.ComponentModel.DataAnnotations;
    using NPoco;
    
    namespace DentsuX.Web.Models
    {
        [TableName("dbo.SkybrudRedirects")]
        [PrimaryKey("Id", AutoIncrement = true)]
    
        public class SkybrudRedirectModel
        {
            public SkybrudRedirectModel()
            {
            }
    
            public int Id { get; set; }
    
            public Guid Key { get; set; }
    
            public int RootId { get; set; }
    
            public Guid RootKey { get; set; }
    
            [MaxLength(255)]
            public string Url { get; set; }
    
            [MaxLength(255)]
            public string QueryString { get; set; }
    
            [MaxLength(255)]
            public string DestinationType { get; set; }
    
            public int DestinationId { get; set; }
    
            public Guid DestinationKey { get; set; }
    
            [MaxLength(255)]
            public string DestinationUrl { get; set; }
    
            public DateTime Created { get; set; }
    
            public DateTime Updated { get; set; }
    
            public bool IsPermanent { get; set; }
    
            public bool IsRegex { get; set; }
    
            public bool ForwardQueryString { get; set; }
        }
    }
    

    Then in a service I made an add method:

    private void AddRedirect(SkybrudRedirectModel model)
            {
                try
                {
                    db.Insert<SkybrudRedirectModel>(model);
                }
                catch (Exception ex)
                {
                    Current.Logger.Error<CultureFallbackService>(string.Format(ErrorLog, "AddRedirect", ex.Message));
                }
            }
    

    Then call that add method (either in the service or from where you hook into the publish event.

    var newRedirect = new SkybrudRedirectModel()
    {
       Key = Guid.NewGuid(),
       RootId = 0,
       RootKey = new Guid(),
       Url = [old url here],
       QueryString = string.Empty,
       DestinationType = "url",
       DestinationId = 0,
       DestinationKey = new Guid(),
       DestinationUrl = [new url here],
       Created = DateTime.Now,
       Updated = DateTime.Now,
       IsPermanent= true,
       IsRegex = false,
       ForwardQueryString = false
       };
    
    this.AddRedirect(newRedirect);
    

    Hope this helps you.

Please Sign in or register to post replies

Write your reply to:

Draft