Skip to content

Commit

Permalink
Fixed some issues with redirects (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdemooij9 committed Jun 1, 2023
1 parent 6b3d856 commit fbcd6f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 25 additions & 6 deletions src/SeoToolkit.Umbraco.Redirects.Core/Services/RedirectsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
{
uri.AbsolutePath.CleanUrl(),
pathAndQuery
};
List<string> 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<string>
{
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<string>();
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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
return item.id;
})
}).then(function (response) {
setItems(response.data);
setItems(response.data.items);
clearSelection();

if (response.status === 200) {
Expand Down

0 comments on commit fbcd6f9

Please sign in to comment.