Skip to content

Commit

Permalink
Merge pull request #28 from unic/feature/fix-preserve-query-string-fo…
Browse files Browse the repository at this point in the history
…r-no-regex

Feature/fix preserve query string for no regex
  • Loading branch information
chaoticm authored Sep 6, 2023
2 parents c3f744c + a7b6afb commit e65ca36
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ Capture groups can be defined when the regex feature has been enabled for a redi

The following term definition matches incoming terms for `global-capture` and transfers all query strings to the target (if any have been provided): `^global-capture([?].*)?`

### Preserve Query String

When this checkbox is checked, the query string from the original request will be passed on to the redirect location. When `Regex enabled` is unchecked, the request url path (excluding the query) has to be an exact match for the `Source term`. If `Source Term` contains a query, then the request url's query string has to start with the query from the `Source Term`.

For example, when `Preserve Query String` is checked, `Regex enabled` is unchecked, and `Source Term` is set to `test`, then this request: `https://mysite.com/test?a=b` will be redirected to the target with the query string included.
If the `Source Term` would be `test?a=b` then this request: `https://mysite.com/test?a=b&c=d` will be a match and redirected to the target with the whole query string included, but this request: `https://mysite.com/test?c=d&a=b` will not be a match.

### Bulk Import

A Sitecore PowerShell script is included allowing Authors to upload a CSV containing redirect definitions.
Expand Down
3 changes: 2 additions & 1 deletion be/src/Unic.UrlMapper2/code/Definitions/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public struct Settings

public struct RegularExpressions
{
public const string QueryStringExpression = "([?].*)?";
public const string QueryStringPattern = "([?].*)?";
public const string PartialQueryStringPattern = "([&].*)?";
}
}
}
4 changes: 2 additions & 2 deletions be/src/Unic.UrlMapper2/code/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.3.1.0")]
[assembly: AssemblyFileVersion("1.3.1.0")]
[assembly: AssemblyVersion("1.3.2.0")]
[assembly: AssemblyFileVersion("1.3.2.0")]
24 changes: 20 additions & 4 deletions be/src/Unic.UrlMapper2/code/Services/RedirectSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,30 @@ protected virtual Redirect MapToSearchResult(RedirectSearchResultItem redirectSe

protected virtual void HandlePreserveQueryString(Redirect redirect, string sourceTerm)
{
if (!redirect.PreserveQueryString) return;
if (!redirect.PreserveQueryString || string.IsNullOrWhiteSpace(sourceTerm)) return;
if (redirect.RegexEnabled)
{
if (!sourceTerm.EndsWith(Constants.RegularExpressions.QueryStringPattern))
{
redirect.Term = sourceTerm + Constants.RegularExpressions.QueryStringPattern;
}

return;
}

// if regex was not enabled we need to make sure that with Preserve Query string we match the exact source term and a query
redirect.RegexEnabled = true;
if (!string.IsNullOrWhiteSpace(sourceTerm)
&& !sourceTerm.EndsWith(Constants.RegularExpressions.QueryStringExpression))
if (!sourceTerm.Contains("?"))
{
redirect.Term = sourceTerm + Constants.RegularExpressions.QueryStringExpression;
redirect.Term = $"^{sourceTerm}{Constants.RegularExpressions.QueryStringPattern}$";
return;
}

var sourceTermPath = sourceTerm.Substring(0, sourceTerm.IndexOf("?", StringComparison.InvariantCultureIgnoreCase));
var addSourceTermQuery = !sourceTerm.EndsWith("?");
var sourceTermQuery = addSourceTermQuery ? sourceTerm.Substring(sourceTerm.IndexOf("?", StringComparison.InvariantCultureIgnoreCase) + 1) : string.Empty;

redirect.Term = $"^{sourceTermPath}{(addSourceTermQuery ? $"([?]{sourceTermQuery}{Constants.RegularExpressions.PartialQueryStringPattern})" : Constants.RegularExpressions.QueryStringPattern)}$";
}

protected virtual IQueryable<RedirectSearchResultItem> GetSearchQuery(IProviderSearchContext searchContext, RedirectSearchData redirectSearchData)
Expand Down

0 comments on commit e65ca36

Please sign in to comment.