Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
[BugFix] Redirects were not working in some cases, for example ([Not …
Browse files Browse the repository at this point in the history
…working - it returns 404](https://our.umbraco.org/projects/developer-tools/301-url-tracker/version-2/64883-Not-working-it-returns-404))

[Improvement] Performance improvement; no SQL queries will be performed anymore for valid requests :-)
  • Loading branch information
kipusoep committed May 22, 2015
1 parent 2d9fb82 commit 592312e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 36 deletions.
113 changes: 79 additions & 34 deletions Modules/UrlTrackerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,9 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa
}
if (rootNodeId == -1)
{
//rootNodeId = -1;
//List<INode> children = new Node(rootNodeId).ChildrenAsList;
//if (children != null && children.Any())
// rootNodeId = children.First().Id;
List<INode> children = new Node(rootNodeId).ChildrenAsList;
if (children != null && children.Any())
rootNodeId = children.First().Id;
}
else
{
Expand Down Expand Up @@ -208,51 +207,97 @@ static void UrlTrackerDo(string callingEventName, bool ignoreHttpStatusCode = fa
string query;
if (!redirectHttpCode.HasValue)
{
// Regex matching
query = "SELECT * FROM icUrlTracker WHERE Is404 = 0 AND ForceRedirect = @forceRedirect AND (RedirectRootNodeId = @redirectRootNodeId OR RedirectRootNodeId = -1) AND OldRegex IS NOT NULL ORDER BY Inserted DESC";
using (IRecordsReader reader = _sqlHelper.ExecuteReader(query, _sqlHelper.CreateParameter("forceRedirect", ignoreHttpStatusCode ? 1 : 0), _sqlHelper.CreateParameter("redirectRootNodeId", rootNodeId)))

if (!ignoreHttpStatusCode)
{
Regex regex;
while (reader.Read())
// Normal matching (database)
// Regex matching
query = "SELECT * FROM icUrlTracker WHERE Is404 = 0 AND ForceRedirect = @forceRedirect AND (RedirectRootNodeId = @redirectRootNodeId OR RedirectRootNodeId = -1) AND OldRegex IS NOT NULL ORDER BY Inserted DESC";
using (IRecordsReader reader = _sqlHelper.ExecuteReader(query, _sqlHelper.CreateParameter("forceRedirect", ignoreHttpStatusCode ? 1 : 0), _sqlHelper.CreateParameter("redirectRootNodeId", rootNodeId)))
{
regex = new Regex(reader.GetString("OldRegex"));
if (regex.IsMatch(url))
Regex regex;
while (reader.Read())
{
LoggingHelper.LogInformation("UrlTracker HttpModule | Regex match found");
if (!reader.IsNull("RedirectNodeId"))
regex = new Regex(reader.GetString("OldRegex"));
if (regex.IsMatch(url))
{
int redirectNodeId = reader.GetInt("RedirectNodeId");
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect node id: {0}", redirectNodeId);
Node n = new Node(redirectNodeId);
if (n != null && n.Name != null && n.Id > 0)
LoggingHelper.LogInformation("UrlTracker HttpModule | Regex match found");
if (!reader.IsNull("RedirectNodeId"))
{
int redirectNodeId = reader.GetInt("RedirectNodeId");
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect node id: {0}", redirectNodeId);
Node n = new Node(redirectNodeId);
if (n != null && n.Name != null && n.Id > 0)
{
redirectUrl = UmbracoHelper.GetUrl(redirectNodeId);
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect url set to: {0}", redirectUrl);
}
else
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect node is invalid; node is null, name is null or id <= 0");
}
else if (!reader.IsNull("RedirectUrl"))
{
redirectUrl = UmbracoHelper.GetUrl(redirectNodeId);
redirectUrl = reader.GetString("RedirectUrl");
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect url set to: {0}", redirectUrl);

if (_capturingGroupsRegex.IsMatch(redirectUrl))
{
LoggingHelper.LogInformation("UrlTracker HttpModule | Found regex capturing groups in the redirect url");
redirectUrl = regex.Replace(url, redirectUrl);

LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect url changed to: {0} (because of regex capturing groups)", redirectUrl);
}
}
else
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect node is invalid; node is null, name is null or id <= 0");

redirectPassThroughQueryString = reader.GetBoolean("RedirectPassThroughQueryString");
LoggingHelper.LogInformation("UrlTracker HttpModule | PassThroughQueryString is enabled");

redirectHttpCode = reader.GetInt("RedirectHttpCode");
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect http code set to: {0}", redirectHttpCode);
}
else if (!reader.IsNull("RedirectUrl"))
}
}
}
else
{
// Forced matching (cache)
List<UrlTrackerModel> forcedRedirects = UrlTrackerRepository.GetForcedRedirects().Where(x => !string.IsNullOrEmpty(x.OldRegex)).ToList();
if (forcedRedirects == null || !forcedRedirects.Any())
return;

foreach (var match in forcedRedirects.Select(x => new { UrlTrackerModel = x, Regex = new Regex(x.OldRegex) }).Where(x => x.Regex.IsMatch(url)))
{
LoggingHelper.LogInformation("UrlTracker HttpModule | Regex match found");
if (match.UrlTrackerModel.RedirectNodeId.HasValue)
{
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect node id: {0}", match.UrlTrackerModel.RedirectNodeId.Value);
Node n = new Node(match.UrlTrackerModel.RedirectNodeId.Value);
if (n != null && n.Name != null && n.Id > 0)
{
redirectUrl = reader.GetString("RedirectUrl");
redirectUrl = UmbracoHelper.GetUrl(match.UrlTrackerModel.RedirectNodeId.Value);
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect url set to: {0}", redirectUrl);
}
else
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect node is invalid; node is null, name is null or id <= 0");
}
else if (!string.IsNullOrEmpty(match.UrlTrackerModel.RedirectUrl))
{
redirectUrl = match.UrlTrackerModel.RedirectUrl;
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect url set to: {0}", redirectUrl);

if (_capturingGroupsRegex.IsMatch(redirectUrl))
{
LoggingHelper.LogInformation("UrlTracker HttpModule | Found regex capturing groups in the redirect url");
redirectUrl = regex.Replace(url, redirectUrl);
if (_capturingGroupsRegex.IsMatch(redirectUrl))
{
LoggingHelper.LogInformation("UrlTracker HttpModule | Found regex capturing groups in the redirect url");
redirectUrl = match.Regex.Replace(url, redirectUrl);

LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect url changed to: {0} (because of regex capturing groups)", redirectUrl);
}
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect url changed to: {0} (because of regex capturing groups)", redirectUrl);
}
}

redirectPassThroughQueryString = reader.GetBoolean("RedirectPassThroughQueryString");
LoggingHelper.LogInformation("UrlTracker HttpModule | PassThroughQueryString is enabled");
redirectPassThroughQueryString = match.UrlTrackerModel.RedirectPassThroughQueryString;
LoggingHelper.LogInformation("UrlTracker HttpModule | PassThroughQueryString is enabled");

redirectHttpCode = reader.GetInt("RedirectHttpCode");
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect http code set to: {0}", redirectHttpCode);
}
redirectHttpCode = match.UrlTrackerModel.RedirectHttpCode;
LoggingHelper.LogInformation("UrlTracker HttpModule | Redirect http code set to: {0}", redirectHttpCode);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("3.7.*")]
[assembly: AssemblyVersion("3.8.*")]

// SQL
[assembly: WebResource("InfoCaster.Umbraco.UrlTracker.SQL.MicrosoftSqlServer.create-table-1.sql", "text/plain")]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Set to true to disable tracking not found (404) requests.
Set to false to disable appending a port number to redirect URLs

## Changelog ##
* 3.8 [2015/05/22]
* [BugFix] Redirects were not working in some cases, for example ([Not working - it returns 404](https://our.umbraco.org/projects/developer-tools/301-url-tracker/version-2/64883-Not-working-it-returns-404))
* [Improvement] Performance improvement; no SQL queries will be performed anymore for valid requests :-)
* 3.7 [2015/05/01]
* [BugFix] Exception on install ([#70](https://github.com/kipusoep/UrlTracker/issues/70))
* 3.6 [2015/03/21]
Expand Down
2 changes: 1 addition & 1 deletion Repositories/UrlTrackerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class UrlTrackerRepository
static readonly Uri _baseUri = new Uri("http://www.example.org");
static List<UrlTrackerModel> _forcedRedirectsCache;
static readonly object _cacheLock = new object();
private static readonly DatabaseProviders DatabaseProvider = ApplicationContext.Current.DatabaseContext.DatabaseProvider;
static readonly DatabaseProviders DatabaseProvider = ApplicationContext.Current.DatabaseContext.DatabaseProvider;

#region Add
public static bool AddUrlMapping(IContent content, int rootNodeId, string url, AutoTrackingTypes type, bool isChild = false)
Expand Down
7 changes: 7 additions & 0 deletions UI/UrlTrackerInfo.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@
</div>
<div class="tab-pane" id="changeLog">
<ul>
<li>
3.8 [2015/05/22]
<ul>
<li>[BugFix] Redirects were not working in some cases, for example <a target="_blank" href="https://our.umbraco.org/projects/developer-tools/301-url-tracker/version-2/64883-Not-working-it-returns-404">Not working - it returns 404</a></li>
<li>[Improvement] Performance improvement; no SQL queries will be performed anymore for valid requests :-)</li>
</ul>
</li>
<li>
3.7 [2015/05/01]
<ul>
Expand Down

0 comments on commit 592312e

Please sign in to comment.