From 702d5197b6f75ac5cea96ebfa281850475a96fd7 Mon Sep 17 00:00:00 2001 From: Patrick de Mooij Date: Wed, 13 Jul 2022 20:28:27 +0200 Subject: [PATCH] Added options IgnoreQueryString and PreserveQueryString --- source/Simple301/Core/Models/Redirect.cs | 22 +++++++++++++++++-- .../Simple301/Core/RedirectContentFinder.cs | 19 ++++++++++++++-- source/Simple301/Core/RedirectUserComposer.cs | 1 + .../Simple301/Core/Utilities/SettingsKeys.cs | 2 ++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/source/Simple301/Core/Models/Redirect.cs b/source/Simple301/Core/Models/Redirect.cs index e0f8faf..f887ec7 100644 --- a/source/Simple301/Core/Models/Redirect.cs +++ b/source/Simple301/Core/Models/Redirect.cs @@ -1,6 +1,8 @@ using NPoco; using System; +using System.Linq; using System.Text.RegularExpressions; +using System.Web; using Umbraco.Core.Persistence.DatabaseAnnotations; namespace SimpleRedirects.Core.Models @@ -32,10 +34,26 @@ public class Redirect [Column("Notes")] public string Notes { get; set; } - public string GetNewUrl(Uri uri) + public string GetNewUrl(Uri uri, bool preserveQueryString) { if (!IsRegex || !NewUrl.Contains($"$")) - return NewUrl; + { + var url = NewUrl; + if (!preserveQueryString || + !Uri.TryCreate(NewUrl, UriKind.RelativeOrAbsolute, out var _)) return url; + + var index = url.IndexOf('?'); + var queryString = index >= 0 ? url.Substring(index) : ""; + var query = HttpUtility.ParseQueryString(queryString); + var appendQuery = HttpUtility.ParseQueryString(uri.Query); + foreach (var item in appendQuery.AllKeys) + { + query[item] = appendQuery.Get(item); + } + + url = $"{url.Split('?').First()}{(query.Count > 0 ? $"?{query}" : string.Empty)}"; + return url; + } try { diff --git a/source/Simple301/Core/RedirectContentFinder.cs b/source/Simple301/Core/RedirectContentFinder.cs index 5b4b77b..08e2239 100644 --- a/source/Simple301/Core/RedirectContentFinder.cs +++ b/source/Simple301/Core/RedirectContentFinder.cs @@ -1,6 +1,8 @@ +using System; using Umbraco.Web.Routing; using System.Linq; using System.Net; +using SimpleRedirects.Core.Utilities; using Umbraco.Core.Composing; namespace SimpleRedirects.Core @@ -13,18 +15,31 @@ public class RedirectContentFinder : IContentFinder { private readonly RedirectRepository _repository; + private readonly bool _ignoreQueryString; + private readonly bool _preserveQueryString; + public RedirectContentFinder(RedirectRepository repository) { _repository = repository; + + var settingsUtility = new SettingsUtility(); + _ignoreQueryString = settingsUtility.AppSettingExists(SettingsKeys.IgnoreQueryString) && settingsUtility.GetAppSetting(SettingsKeys.IgnoreQueryString); + _preserveQueryString = settingsUtility.AppSettingExists(SettingsKeys.PreserveQueryString) && settingsUtility.GetAppSetting(SettingsKeys.PreserveQueryString); } public bool TryFindContent(PublishedRequest request) { + var uri = request.Uri; + if (_ignoreQueryString) + { + uri = new Uri(uri.AbsoluteUri.Split('?').First()); + } + //Check the table - var matchedRedirect = _repository.FindRedirect(request.Uri); + var matchedRedirect = _repository.FindRedirect(uri); if (matchedRedirect == null) return false; - request.SetRedirect(matchedRedirect.GetNewUrl(request.Uri), matchedRedirect.RedirectCode); + request.SetRedirect(matchedRedirect.GetNewUrl(request.Uri, _preserveQueryString), matchedRedirect.RedirectCode); return true; } diff --git a/source/Simple301/Core/RedirectUserComposer.cs b/source/Simple301/Core/RedirectUserComposer.cs index f328814..17ddd43 100644 --- a/source/Simple301/Core/RedirectUserComposer.cs +++ b/source/Simple301/Core/RedirectUserComposer.cs @@ -7,6 +7,7 @@ namespace SimpleRedirects.Core { + [RuntimeLevel(MinLevel =RuntimeLevel.Run)] public class RedirectUserComposer : IUserComposer { public void Compose(Composition composition) diff --git a/source/Simple301/Core/Utilities/SettingsKeys.cs b/source/Simple301/Core/Utilities/SettingsKeys.cs index 5300dbc..f2fd23e 100644 --- a/source/Simple301/Core/Utilities/SettingsKeys.cs +++ b/source/Simple301/Core/Utilities/SettingsKeys.cs @@ -7,5 +7,7 @@ public static class SettingsKeys { public static string CacheDurationKey = "SimpleRedirects.CacheDurationInSeconds"; public static string CacheEnabledKey = "SimpleRedirects.CacheEnabled"; + public static string IgnoreQueryString = "SimpleRedirects.IgnoreQueryString"; + public static string PreserveQueryString = "SimpleRedirects.PreserveQueryString"; } }