From fbcd6f9b573c86229d7c0c468e3839c1ce203345 Mon Sep 17 00:00:00 2001 From: patrickdemooij9 Date: Thu, 1 Jun 2023 20:05:03 +0200 Subject: [PATCH] Fixed some issues with redirects (#200) --- .../Services/RedirectsService.cs | 31 +++++++++++++++---- .../backoffice/Redirects/list.controller.js | 2 +- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs b/src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs index 95fbf466..5bb6a080 100644 --- a/src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs +++ b/src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs @@ -91,19 +91,39 @@ public RedirectFindResult GetByUrl(Uri uri) using (var ctx = _umbracoContextFactory.EnsureUmbracoContext()) { var domain = DomainUtilities.SelectDomain(ctx.UmbracoContext.Domains.GetAll(false), uri); + + var pathAndQuery = uri.PathAndQuery.CleanUrl(); + var globalUrls = new List + { + uri.AbsolutePath.CleanUrl(), + pathAndQuery + }; + List domainUrls = null; + if (domain != null) { //We do this to ensure that we support subdirectories. So if you have domain domain.com/en and relative path /test123, you don't also need to include the subdirectory /en/test123. - uri = new Uri(domain.Uri, uri.AbsolutePath.TrimStart(domain.Uri.LocalPath)); + var domainUrl = uri.AbsolutePath.CleanUrl().TrimStart(domain.Uri.LocalPath); + + domainUrls = new List + { + domainUrl, + $"{domainUrl}{uri.Query}" + }; } - var path = uri.AbsolutePath.CleanUrl(); - var pathAndQuery = uri.PathAndQuery.CleanUrl(); var customDomainWithoutScheme = uri.Host; var customDomainWithScheme = $"{uri.Scheme}://{uri.Host}"; + var urlsToSearch = new List(); + urlsToSearch.AddRange(globalUrls); + if (domainUrls != null) + { + urlsToSearch.AddRange(domainUrls); + } + //Because we are checking both the url with and without query, we might get two urls. - var redirects = _redirectsRepository.GetByUrls(path, pathAndQuery).ToArray(); + var redirects = _redirectsRepository.GetByUrls(urlsToSearch.Distinct().ToArray()).ToArray(); if (redirects.Length > 0) { Redirect foundRedirect = null; @@ -125,8 +145,7 @@ public RedirectFindResult GetByUrl(Uri uri) foundRedirect = redirects.FirstOrDefault(it => it.Domain is null && string.IsNullOrWhiteSpace(it.CustomDomain) && - (it.OldUrl.Equals(path, StringComparison.InvariantCultureIgnoreCase) || - it.OldUrl.Equals(pathAndQuery, StringComparison.InvariantCultureIgnoreCase))); + globalUrls.Contains(it.OldUrl, StringComparer.InvariantCultureIgnoreCase)); if (foundRedirect != null) return new RedirectFindResult(uri, foundRedirect); } diff --git a/src/SeoToolkit.Umbraco.Redirects/wwwroot/backoffice/Redirects/list.controller.js b/src/SeoToolkit.Umbraco.Redirects/wwwroot/backoffice/Redirects/list.controller.js index df6e6c50..d6c2cc73 100644 --- a/src/SeoToolkit.Umbraco.Redirects/wwwroot/backoffice/Redirects/list.controller.js +++ b/src/SeoToolkit.Umbraco.Redirects/wwwroot/backoffice/Redirects/list.controller.js @@ -165,7 +165,7 @@ return item.id; }) }).then(function (response) { - setItems(response.data); + setItems(response.data.items); clearSelection(); if (response.status === 200) {