Skip to content

Commit e65ca36

Browse files
authored
Merge pull request #28 from unic/feature/fix-preserve-query-string-for-no-regex
Feature/fix preserve query string for no regex
2 parents c3f744c + a7b6afb commit e65ca36

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ Capture groups can be defined when the regex feature has been enabled for a redi
3030

3131
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([?].*)?`
3232

33+
### Preserve Query String
34+
35+
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`.
36+
37+
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.
38+
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.
39+
3340
### Bulk Import
3441

3542
A Sitecore PowerShell script is included allowing Authors to upload a CSV containing redirect definitions.

be/src/Unic.UrlMapper2/code/Definitions/Constants.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public struct Settings
5858

5959
public struct RegularExpressions
6060
{
61-
public const string QueryStringExpression = "([?].*)?";
61+
public const string QueryStringPattern = "([?].*)?";
62+
public const string PartialQueryStringPattern = "([&].*)?";
6263
}
6364
}
6465
}

be/src/Unic.UrlMapper2/code/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
//
3131
// You can specify all the values or you can default the Revision and Build Numbers
3232
// by using the '*' as shown below:
33-
[assembly: AssemblyVersion("1.3.1.0")]
34-
[assembly: AssemblyFileVersion("1.3.1.0")]
33+
[assembly: AssemblyVersion("1.3.2.0")]
34+
[assembly: AssemblyFileVersion("1.3.2.0")]

be/src/Unic.UrlMapper2/code/Services/RedirectSearcher.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,30 @@ protected virtual Redirect MapToSearchResult(RedirectSearchResultItem redirectSe
8181

8282
protected virtual void HandlePreserveQueryString(Redirect redirect, string sourceTerm)
8383
{
84-
if (!redirect.PreserveQueryString) return;
84+
if (!redirect.PreserveQueryString || string.IsNullOrWhiteSpace(sourceTerm)) return;
85+
if (redirect.RegexEnabled)
86+
{
87+
if (!sourceTerm.EndsWith(Constants.RegularExpressions.QueryStringPattern))
88+
{
89+
redirect.Term = sourceTerm + Constants.RegularExpressions.QueryStringPattern;
90+
}
91+
92+
return;
93+
}
8594

95+
// if regex was not enabled we need to make sure that with Preserve Query string we match the exact source term and a query
8696
redirect.RegexEnabled = true;
87-
if (!string.IsNullOrWhiteSpace(sourceTerm)
88-
&& !sourceTerm.EndsWith(Constants.RegularExpressions.QueryStringExpression))
97+
if (!sourceTerm.Contains("?"))
8998
{
90-
redirect.Term = sourceTerm + Constants.RegularExpressions.QueryStringExpression;
99+
redirect.Term = $"^{sourceTerm}{Constants.RegularExpressions.QueryStringPattern}$";
100+
return;
91101
}
102+
103+
var sourceTermPath = sourceTerm.Substring(0, sourceTerm.IndexOf("?", StringComparison.InvariantCultureIgnoreCase));
104+
var addSourceTermQuery = !sourceTerm.EndsWith("?");
105+
var sourceTermQuery = addSourceTermQuery ? sourceTerm.Substring(sourceTerm.IndexOf("?", StringComparison.InvariantCultureIgnoreCase) + 1) : string.Empty;
106+
107+
redirect.Term = $"^{sourceTermPath}{(addSourceTermQuery ? $"([?]{sourceTermQuery}{Constants.RegularExpressions.PartialQueryStringPattern})" : Constants.RegularExpressions.QueryStringPattern)}$";
92108
}
93109

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

0 commit comments

Comments
 (0)