diff --git a/src/Umbraco.Web/Routing/ContentFinderByRedirectUrl.cs b/src/Umbraco.Web/Routing/ContentFinderByRedirectUrl.cs new file mode 100644 index 0000000000..0e112fdbe9 --- /dev/null +++ b/src/Umbraco.Web/Routing/ContentFinderByRedirectUrl.cs @@ -0,0 +1,67 @@ +namespace Umbraco.Web.Routing +{ + using Umbraco.Core; + using Umbraco.Core.Logging; + + /// + /// Provides an implementation of that handles page url rewrites + /// that are stored when moving, saving, or deleting a node. + /// + /// + /// Assigns a permanent redirect notification to the request. + /// + public class ContentFinderByRedirectUrl : IContentFinder + { + /// + /// Tries to find and assign an Umbraco document to a PublishedContentRequest. + /// + /// The PublishedContentRequest. + /// A value indicating whether an Umbraco document was found and assigned. + /// Optionally, can also assign the template or anything else on the document request, although that is not required. + public bool TryFindContent(PublishedContentRequest contentRequest) + { + string route; + if (contentRequest.HasDomain) + { + route = contentRequest.UmbracoDomain.RootContentId + DomainHelper.PathRelativeToDomain(contentRequest.DomainUri, contentRequest.Uri.GetAbsolutePathDecoded()); + } + else + { + route = contentRequest.Uri.GetAbsolutePathDecoded(); + } + + return this.FindContent(contentRequest, route); + } + + /// + /// Tries to find an Umbraco document for a PublishedContentRequest and a route. + /// + /// The document request. + /// The route. + /// True if a redirect is to take place, otherwise; false. + protected bool FindContent(PublishedContentRequest contentRequest, string route) + { + var rule = contentRequest.RoutingContext + .UmbracoContext.Application.Services + .RedirectUrlService.GetMostRecentRule(route); + + if (rule != null) + { + var content = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(rule.ContentId); + if (content != null) + { + var url = content.Url; + if (url != "#") + { + contentRequest.SetRedirectPermanent(url); + LogHelper.Debug("Got content, id={0}", () => content.Id); + return true; + } + } + } + + LogHelper.Debug("No match for the url: {0}.", () => route); + return false; + } + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index de4b49bc77..3ca753db63 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -360,6 +360,7 @@ +