Copied to clipboard

Flag this post as spam?

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


  • Nathan 67 posts 146 karma points
    Sep 19, 2018 @ 12:31
    Nathan
    0

    Manually add redirects to Umbraco's built in URL Redirect Management

    Hi all,

    How does Umbraco's built-in URL Redirect Management work behind the scenes?

    umbracoRedirectUrl table enter image description here

    Is it possible to manually add entries into the table knowing old url and new url or nodeID etc?

    Thanks

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Sep 19, 2018 @ 13:03
    Dave Woestenborghs
    0

    Hi Ivan,

    My Redirects viewer package allows you to do that on a content item.

    https://our.umbraco.com/packages/backoffice-extensions/redirects-viewer/

    And here you can find a blogpost by Marc Goodson about redirects : http://tooorangey.co.uk/posts/301-redirection-in-umbraco-it-s-a-rum-do/

    Dave

  • Nathan 67 posts 146 karma points
    Sep 19, 2018 @ 13:10
    Nathan
    0

    Hi Dave,

    Thanks for this.

    Does it work with the built-in functionality of Umbraco? adding entries to the existing umbracoRedirectUrl table?

    Thanks

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Sep 19, 2018 @ 13:20
    Dave Woestenborghs
    0

    Hi Ivan,

    It does...I build this package with this feature in mind. I often got questions from editors that they could not see the redirects configured for an item. And of course than they ask how to add and delete them.

    Adding and deleting is configurable by the way based on user groups in the configuration of the datatype.

    Dave

  • Nathan 67 posts 146 karma points
    Sep 19, 2018 @ 13:22
    Nathan
    0

    ok many thanks will give it a try and get back to you.

  • Nathan 67 posts 146 karma points
    Sep 26, 2018 @ 14:34
    Nathan
    0

    Hi Dave,

    We've installed this package and it works fine with future URL modifications.

    However, we have about 500 redirects created via the 301 URL Tracker plugin and we need to move them and populate the umbracoRedirectUrl table via code. We've followed this post:

    https://our.umbraco.com/forum/using-umbraco-and-getting-started/82460-can-i-populate-umbracoredirecturl-manually

    It looks like you call the ToSHA1() function to generate the hash: https://github.com/umbraco/Umbraco-CMS/blob/5397f2c53acbdeb0805e1fe39fda938f571d295a/src/Umbraco.Core/Persistence/Repositories/RedirectUrlRepository.cs#L108

    For the ID, you can probably just create a new GUID with Guid.NewGuid().

    FYI, to use the ToSHA1() extension method, you'll need to use the namespace Umbraco.Core: https://github.com/umbraco/Umbraco-CMS/blob/26a04ca7e91750a796c753f2f071cb1af8045cb1/src/Umbraco.Core/StringExtensions.cs#L739

    The records seem to be added correctly into the umbracoRedirectUrl table, these appear on every page and in the dashboard.

    But the redirects are not working.

    Are we missing something?

    See below the code:

        SqlDataReader tableUrlTracker = null;
        try
        {
            myIRedirectUrl mode = new myIRedirectUrl();
    
    
            SqlConnection DBConnToUMB = new SqlConnection();
    
            DBConnToUMB = new SqlConnection(ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString);
    
            DBConnToUMB.Open();
    
    
    
            StringBuilder insertScript = new StringBuilder();
    
            tableUrlTracker = new SqlCommand("SELECT * FROM icUrlTracker where is404 = 0", DBConnToUMB).ExecuteReader();
            if (tableUrlTracker.HasRows)
            {
    
                while (tableUrlTracker.Read())
                {
                    string oldUrl ="1928/"+ tableUrlTracker["OldUrl"];
                    DateTime createDateUtc =(System.DateTime)tableUrlTracker["Inserted"];
    
                    string nodeUniqueId = "";
                    int pageId=0;
                    if (tableUrlTracker["RedirectNodeId"] != null && !string.IsNullOrEmpty(tableUrlTracker["RedirectNodeId"].ToString() ))
                    {
    
                        int.TryParse(tableUrlTracker["RedirectNodeId"].ToString(), out pageId);
    
                        SqlDataReader tableUmbracoNode = new SqlCommand("SELECT top(1) * FROM umbracoNode  where id ="+ pageId.ToString(), DBConnToUMB).ExecuteReader();
                        if (tableUmbracoNode.HasRows)
                        {
                            while (tableUmbracoNode.Read())
                            {
                                nodeUniqueId = tableUmbracoNode["uniqueID"].ToString();
                                continue;
                            }
                        }
                        tableUmbracoNode.Close();
    
                    }
                    if (pageId== 0)
                    {
                        continue;
                    }
    
                    string redirectUrl = "";
                    try
                    {
                        redirectUrl = new DynamicNode(pageId).Url;
    
                        if (nodeUniqueId == "" || redirectUrl == null)
                        {
                            continue;
                        }
                    }
                    catch
                    {
                        continue;
                    }
    
                    string encodedUrl = redirectUrl.ToSHA1();
                    string urlId = Guid.NewGuid().ToString();
                    insertScript.AppendLine("INSERT INTO [dbo].[umbracoRedirectUrl] ([id] ,[createDateUtc] ,[url] ,[contentKey] ,[urlHash]) VALUES('" + urlId + "','" + createDateUtc.ToString("yyyy-MM-dd HH:mm:ss") + "','" + oldUrl + "','" + nodeUniqueId + "','" + encodedUrl + "')").Append(Environment.NewLine);
                    insertScript.Append(Environment.NewLine);
                }
            }
    
            insertScript.AppendLine();
    
    
        }
        catch(Exception e)
        {
    
            string s = e.Message;
        }
        finally
        {
            tableUrlTracker.Close();
    
        }
    

    Any ideas why these redirects are not working?

    Thanks

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Sep 26, 2018 @ 14:40
    Dave Woestenborghs
    0

    Hi Ivan,

    I never tried importing from another redirect tool to the Umbraco Redirect table.

    So unfortunatly I can't help you...I never used the 301 Url Tracker as well.

    Dave

  • Henrik Bayer Nielsen 8 posts 89 karma points
    Oct 25, 2019 @ 07:58
    Henrik Bayer Nielsen
    1

    If anyone has this problem again I fixed Nathan's code by replacing

    string encodedUrl = redirectUrl.ToSHA1();
    

    With

     string encodedUrl = oldUrl.GenerateSha1Hash();
    

    That made it work for me and I could import all the url's from 301 Url Tracker.

    Umbraco 7.15.1

Please Sign in or register to post replies

Write your reply to:

Draft