From efe5d0f5aca16b01fa8520d29d4f21cdaf1ef017 Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Wed, 2 Feb 2022 12:39:52 +0100 Subject: [PATCH] Updated "NormalizeUrlParts" method to handle a few more edge cases --- .../RedirectsUtils.cs | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Skybrud.Umbraco.Redirects/RedirectsUtils.cs b/src/Skybrud.Umbraco.Redirects/RedirectsUtils.cs index 73b811d..7b14278 100644 --- a/src/Skybrud.Umbraco.Redirects/RedirectsUtils.cs +++ b/src/Skybrud.Umbraco.Redirects/RedirectsUtils.cs @@ -2,15 +2,15 @@ using Skybrud.Umbraco.Redirects.Models; namespace Skybrud.Umbraco.Redirects { - + internal class RedirectsUtils { public static bool NormalizeUrlParts(RedirectItem redirect) { string url = redirect.LinkUrl; - string query = null; + string query = redirect.LinkQuery; string fragment = null; - + // Isolate the fragment if specified in the URL int pos1 = url.IndexOf('#'); if (pos1 >= 0) { @@ -21,25 +21,28 @@ public static bool NormalizeUrlParts(RedirectItem redirect) { // Isolate the query string if specified in the URL int pos2 = url.IndexOf('?'); if (pos2 >= 0) { - query = url.Substring(pos2 + 1); + query += "&" + url.Substring(pos2 + 1); url = url.Substring(0, pos2); } // Parse the "fragment" value if (redirect.LinkFragment.HasValue()) { + string temp = redirect.LinkFragment; + // Isolate the fragment if specified in the "anchor" value (overwrites fragment from the URL) - var pos3 = redirect.LinkFragment.IndexOf('#'); + var pos3 = temp.IndexOf('#'); if (pos3 >= 0) { - fragment = redirect.LinkFragment.Substring(pos3); + fragment = temp.Substring(pos3); + temp = pos3 > 0 ? temp.Substring(0, pos3 - 1) : string.Empty; } // Treat remaining anchor value as query string (append if URL also has query string) - if (redirect.LinkFragment.HasValue()) { - if (redirect.LinkFragment.IndexOf('?') == 0 || redirect.LinkFragment.IndexOf('&') == 0) { - query += "&" + redirect.LinkFragment.Substring(1); + if (temp.HasValue()) { + if (temp.IndexOf('?') == 0 || temp.IndexOf('&') == 0) { + query += "&" + temp.Substring(1); } else { - query += "&" + redirect.LinkFragment; + query += "&" + temp; } }