Skip to content
This repository was archived by the owner on Apr 14, 2019. It is now read-only.
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 package/Web.config.install.xdt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<appSettings>
<add key="Simple301.CacheDurationInSeconds" value="3600" xdt:Transform="Insert"/>
<add key="Simple301.CacheEnabled" value="true" xdt:Transform="Insert"/>
<add key="Simple301.CustomRedirectContentFinder" value="false" xdt:Transform="Insert"/>
</appSettings>
</configuration>
1 change: 1 addition & 0 deletions package/Web.config.uninstall.xdt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<appSettings>
<add key="Simple301.CacheDurationInSeconds" value="3600" xdt:Locator="Match(key)" xdt:Transform="Remove"/>
<add key="Simple301.CacheEnabled" value="true" xdt:Locator="Match(key)" xdt:Transform="Remove"/>
<add key="Simple301.CustomRedirectContentFinder" value="false" xdt:Locator="Match(key)" xdt:Transform="Remove"/>
</appSettings>
</configuration>
12 changes: 11 additions & 1 deletion source/Simple301/Core/RedirectApplicationEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Umbraco.Core.Persistence.Migrations;
using System;
using System.Web;
using Simple301.Core.Utilities;
using Umbraco.Core.Services;

namespace Simple301.Core
Expand All @@ -23,7 +24,16 @@ public class MyApplication : ApplicationEventHandler
/// </summary>
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentFinderResolver.Current.InsertType<RedirectContentFinder>(0);
var settingsUtility = new SettingsUtility();

var customRedirectContentFinder = settingsUtility.AppSettingExists(SettingsKeys.CustomRedirectContentFinderKey) ?
settingsUtility.GetAppSetting<bool>(SettingsKeys.CustomRedirectContentFinderKey) :
false;

if (!customRedirectContentFinder)
{
ContentFinderResolver.Current.InsertType<RedirectContentFinder>(0);
}
}

/// <summary>
Expand Down
18 changes: 17 additions & 1 deletion source/Simple301/Core/RedirectContentFinder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Umbraco.Web.Routing;
using System.Linq;
using System.Text.RegularExpressions;

namespace Simple301.Core
{
Expand All @@ -18,6 +18,22 @@ public bool TryFindContent(PublishedContentRequest request)
var matchedRedirect = RedirectRepository.FindRedirect(path);
if (matchedRedirect == null) return false;

// Groups match replace
string newUrl = matchedRedirect.NewUrl;

if (matchedRedirect.IsRegex && matchedRedirect.OldUrl.Contains("(.*)"))
{
var match = Regex.Match(path, matchedRedirect.OldUrl);

if (match.Groups.Count > 1)
{
for (int iGrp = 1; iGrp < match.Groups.Count; iGrp++)
{
newUrl = newUrl.Replace($"${iGrp}", match.Groups[iGrp].Value);
}
}
}

//Found one, set the 301 redirect on the request and return
request.SetRedirectPermanent(matchedRedirect.NewUrl);
return true;
Expand Down
1 change: 1 addition & 0 deletions source/Simple301/Core/Utilities/SettingsKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public static class SettingsKeys
{
public static string CacheDurationKey = "Simple301.CacheDurationInSeconds";
public static string CacheEnabledKey = "Simple301.CacheEnabled";
public static string CustomRedirectContentFinderKey = "Simple301.CustomRedirectContentFinder";
}
}