Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider query string field for internal redirects #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## files generated by popular Visual Studio add-ons.

# User-specific files
.vs/
*.suo
*.user
*.sln.docstates
Expand Down
1 change: 1 addition & 0 deletions Hi.UrlRewrite/Entities/Actions/Base/IBaseRewriteUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IBaseRewriteUrl
{
string RewriteUrl { get; set; }
Guid? RewriteItemId { get; set; }
string RewriteItemQueryString { get; set; }
string RewriteItemAnchor { get; set; }
}
}
1 change: 1 addition & 0 deletions Hi.UrlRewrite/Entities/Actions/Redirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Redirect : IBaseRedirect
public string Name { get; set; }
public string RewriteUrl { get; set; }
public Guid? RewriteItemId { get; set; }
public string RewriteItemQueryString { get; set; }
public string RewriteItemAnchor { get; set; }
public bool AppendQueryString { get; set; }

Expand Down
3 changes: 2 additions & 1 deletion Hi.UrlRewrite/Entities/Actions/Rewrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Rewrite : IBaseRewrite
public bool AppendQueryString { get; set; }
public string RewriteUrl { get; set; }
public Guid? RewriteItemId { get; set; }
public string RewriteItemQueryString { get; set; }
public string RewriteItemAnchor { get; set; }
}
}
}
4 changes: 3 additions & 1 deletion Hi.UrlRewrite/Extensions/ItemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,10 +786,12 @@ private static void GetBaseRewriteUrlItem(BaseRewriteUrlItem baseRewriteUrlItem,
var redirectTo = baseRewriteUrlItem.RewriteUrl;
string actionRewriteUrl;
Guid? redirectItemId;
string redirectItemQueryString;
string redirectItemAnchor;

RulesEngine.GetRedirectUrlOrItemId(redirectTo, out actionRewriteUrl, out redirectItemId, out redirectItemAnchor);
RulesEngine.GetRedirectUrlOrItemId(redirectTo, out actionRewriteUrl, out redirectItemId, out redirectItemQueryString, out redirectItemAnchor);
redirectAction.RewriteItemId = redirectItemId;
redirectAction.RewriteItemQueryString = redirectItemQueryString;
redirectAction.RewriteItemAnchor = redirectItemAnchor;
redirectAction.RewriteUrl = actionRewriteUrl;
}
Expand Down
23 changes: 17 additions & 6 deletions Hi.UrlRewrite/Processing/InboundRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ private void ProcessRedirectAction(InboundRule inboundRule, Uri uri, Match inbou

var rewriteUrl = redirectAction.RewriteUrl;
var rewriteItemId = redirectAction.RewriteItemId;
var rewriteItemQueryString = redirectAction.RewriteItemQueryString;
var rewriteItemAnchor = redirectAction.RewriteItemAnchor;

if (string.IsNullOrEmpty(rewriteUrl) && rewriteItemId == null)
Expand All @@ -398,7 +399,7 @@ private void ProcessRedirectAction(InboundRule inboundRule, Uri uri, Match inbou

if (rewriteItemId.HasValue)
{
rewriteUrl = GetRewriteUrlFromItemId(rewriteItemId.Value, rewriteItemAnchor);
rewriteUrl = GetRewriteUrlFromItemId(rewriteItemId.Value, rewriteItemQueryString, rewriteItemAnchor);
}


Expand All @@ -413,7 +414,9 @@ private void ProcessRedirectAction(InboundRule inboundRule, Uri uri, Match inbou

if (redirectAction.AppendQueryString)
{
rewriteUrl += uri.Query;
rewriteUrl += rewriteUrl.Contains("?")
? uri.Query.Replace("?", "&")
: uri.Query;
}

rewriteUrl = RewriteHelper.ReplaceRuleBackReferences(inboundRuleMatch, rewriteUrl);
Expand All @@ -429,6 +432,7 @@ private void ProcessRewriteAction(InboundRule inboundRule, Uri uri, Match inboun

var rewriteUrl = redirectAction.RewriteUrl;
var rewriteItemId = redirectAction.RewriteItemId;
var rewriteItemQueryString = redirectAction.RewriteItemQueryString;
var rewriteItemAnchor = redirectAction.RewriteItemAnchor;

if (string.IsNullOrEmpty(rewriteUrl) && rewriteItemId == null)
Expand All @@ -439,7 +443,7 @@ private void ProcessRewriteAction(InboundRule inboundRule, Uri uri, Match inboun

if (rewriteItemId.HasValue)
{
rewriteUrl = GetRewriteUrlFromItemId(rewriteItemId.Value, rewriteItemAnchor);
rewriteUrl = GetRewriteUrlFromItemId(rewriteItemId.Value, rewriteItemQueryString, rewriteItemAnchor);
}

// process token replacements
Expand All @@ -453,7 +457,9 @@ private void ProcessRewriteAction(InboundRule inboundRule, Uri uri, Match inboun

if (redirectAction.AppendQueryString)
{
rewriteUrl += uri.Query;
rewriteUrl += rewriteUrl.Contains("?")
? uri.Query.Replace("?", "&")
: uri.Query;
}

rewriteUrl = RewriteHelper.ReplaceRuleBackReferences(inboundRuleMatch, rewriteUrl);
Expand Down Expand Up @@ -487,7 +493,7 @@ private void ProcessItemQueryRedirectAction(InboundRule inboundRule, Uri uri, Ma
return;
}

string rewriteUrl = GetRewriteUrlFromItemId(rewriteItemId.Value, null);
string rewriteUrl = GetRewriteUrlFromItemId(rewriteItemId.Value, null, null);


// process token replacements
Expand Down Expand Up @@ -517,7 +523,7 @@ private void ProcessItemQueryRedirectAction(InboundRule inboundRule, Uri uri, Ma
return null;
}

private string GetRewriteUrlFromItemId(Guid rewriteItemId, string rewriteItemAnchor)
private string GetRewriteUrlFromItemId(Guid rewriteItemId, string rewriteItemQueryString, string rewriteItemAnchor)
{
string rewriteUrl = null;

Expand Down Expand Up @@ -546,6 +552,11 @@ private string GetRewriteUrlFromItemId(Guid rewriteItemId, string rewriteItemAnc
rewriteUrl = LinkManager.GetItemUrl(rewriteItem, urlOptions);
}

if (!string.IsNullOrEmpty(rewriteItemQueryString))
{
rewriteUrl += string.Format("?{0}", rewriteItemQueryString);
}

if (!string.IsNullOrEmpty(rewriteItemAnchor))
{
rewriteUrl += string.Format("#{0}", rewriteItemAnchor);
Expand Down
8 changes: 6 additions & 2 deletions Hi.UrlRewrite/Processing/RulesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ internal InboundRule CreateInboundRuleFromSimpleRedirectItem(SimpleRedirectItem
var redirectTo = simpleRedirectItem.Target;
string actionRewriteUrl;
Guid? redirectItem;
string redirectItemQueryString;
string redirectItemAnchor;

GetRedirectUrlOrItemId(redirectTo, out actionRewriteUrl, out redirectItem, out redirectItemAnchor);
GetRedirectUrlOrItemId(redirectTo, out actionRewriteUrl, out redirectItem, out redirectItemQueryString, out redirectItemAnchor);

Log.Debug(this, simpleRedirectItem.Database, "Creating Inbound Rule From Simple Redirect Item - {0} - id: {1} actionRewriteUrl: {2} redirectItem: {3}", simpleRedirectItem.Name, simpleRedirectItem.ID.Guid, actionRewriteUrl, redirectItem);

Expand All @@ -177,6 +178,7 @@ internal InboundRule CreateInboundRuleFromSimpleRedirectItem(SimpleRedirectItem
StatusCode = RedirectStatusCode.Permanent,
RewriteUrl = actionRewriteUrl,
RewriteItemId = redirectItem,
RewriteItemQueryString = redirectItemQueryString,
RewriteItemAnchor = redirectItemAnchor,
StopProcessingOfSubsequentRules = false,
HttpCacheability = HttpCacheability.NoCache
Expand Down Expand Up @@ -219,15 +221,17 @@ internal static string GetSiteNameRestriction(RedirectFolderItem redirectFolder)
return site;
}

internal static void GetRedirectUrlOrItemId(LinkField redirectTo, out string actionRewriteUrl, out Guid? redirectItemId, out string redirectItemAnchor)
internal static void GetRedirectUrlOrItemId(LinkField redirectTo, out string actionRewriteUrl, out Guid? redirectItemId, out string redirectItemQueryString, out string redirectItemAnchor)
{
actionRewriteUrl = null;
redirectItemId = null;
redirectItemQueryString = null;
redirectItemAnchor = null;

if (redirectTo.TargetItem != null)
{
redirectItemId = redirectTo.TargetItem.ID.Guid;
redirectItemQueryString = redirectTo.QueryString;
redirectItemAnchor = redirectTo.Anchor;
}
else
Expand Down
1 change: 1 addition & 0 deletions Hi.UrlRewrite/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<packages>
<package id="bootstrap" version="3.3.1" targetFramework="net45" />
<package id="jQuery" version="1.9.0" targetFramework="net45" />
<package id="Sitecore.Analytics.Aggregation.NoReferences" version="8.2.160729" targetFramework="net452" developmentDependency="true" />
<package id="Sitecore.Analytics.Core.NoReferences" version="8.2.160729" targetFramework="net452" developmentDependency="true" />
<package id="Sitecore.Analytics.Model.NoReferences" version="8.2.160729" targetFramework="net452" developmentDependency="true" />
<package id="Sitecore.Analytics.NoReferences" version="8.2.160729" targetFramework="net452" developmentDependency="true" />
Expand Down