Skip to content

Commit

Permalink
Added options IgnoreQueryString and PreserveQueryString
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdemooij9 committed Jul 13, 2022
1 parent 287d10b commit 702d519
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
22 changes: 20 additions & 2 deletions source/Simple301/Core/Models/Redirect.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
{
Expand Down
19 changes: 17 additions & 2 deletions source/Simple301/Core/RedirectContentFinder.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<bool>(SettingsKeys.IgnoreQueryString);
_preserveQueryString = settingsUtility.AppSettingExists(SettingsKeys.PreserveQueryString) && settingsUtility.GetAppSetting<bool>(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;
}
Expand Down
1 change: 1 addition & 0 deletions source/Simple301/Core/RedirectUserComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace SimpleRedirects.Core
{
[RuntimeLevel(MinLevel =RuntimeLevel.Run)]
public class RedirectUserComposer : IUserComposer
{
public void Compose(Composition composition)
Expand Down
2 changes: 2 additions & 0 deletions source/Simple301/Core/Utilities/SettingsKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}

0 comments on commit 702d519

Please sign in to comment.