protected virtual RedirectMapping FindMapping(string filePath) { RedirectMapping redirectMapping = null; List <RedirectMapping> .Enumerator enumerator = this.MappingsMap.GetEnumerator(); try { while (enumerator.MoveNext()) { RedirectMapping current = enumerator.Current; if ((current.IsRegex || !(current.Pattern == filePath)) && (!current.IsRegex || !current.Regex.IsMatch(filePath))) { continue; } redirectMapping = current; return(redirectMapping); } return(null); } catch (Exception) { } finally { ((IDisposable)enumerator).Dispose(); } return(redirectMapping); }
public override void Process(HttpRequestArgs args) { if (Context.Item != null || Context.Database == null || Context.Site == null || this.IsFile(Context.Request.FilePath)) { return; } if (Context.PageMode.IsExperienceEditor || Context.PageMode.IsPreview) { return; } string str = this.EnsureSlashes(Context.Request.FilePath.ToLower()); var lang = Context.Language.Name == "en" ? "us" : Context.Language.Name; //Log.Error($"Method => Process / str => {str}", this); //Log.Error($"Method => Process / Context.Language.Name => {lang}", this); str = Regex.Split(str, $"/{lang}", RegexOptions.IgnoreCase).LastOrDefault(); RedirectMapping resolvedMapping = this.GetResolvedMapping(str); bool flag = resolvedMapping != null; if (resolvedMapping == null) { resolvedMapping = this.FindMapping(str); } if (resolvedMapping != null && !flag) { Dictionary <string, RedirectMapping> item = HttpRuntime.Cache[this.ResolvedMappingsPrefix] as Dictionary <string, RedirectMapping> ?? new Dictionary <string, RedirectMapping>(); item[str] = resolvedMapping; Cache cache = HttpRuntime.Cache; string resolvedMappingsPrefix = this.ResolvedMappingsPrefix; DateTime utcNow = DateTime.UtcNow; cache.Add(resolvedMappingsPrefix, item, null, utcNow.AddMinutes((double)this.CacheExpiration), TimeSpan.Zero, CacheItemPriority.Normal, null); } if (resolvedMapping != null && HttpContext.Current != null) { string targetUrl = this.GetTargetUrl(resolvedMapping, str); if (resolvedMapping.RedirectType == RedirectType.Redirect301) { this.Redirect301(HttpContext.Current.Response, targetUrl); } if (resolvedMapping.RedirectType == RedirectType.Redirect302) { HttpContext.Current.Response.Redirect(targetUrl, true); } if (resolvedMapping.RedirectType == RedirectType.ServerTransfer) { HttpContext.Current.Server.TransferRequest(targetUrl); } } }
protected virtual string GetTargetUrl(RedirectMapping mapping, string input) { string target = mapping.Target; if (mapping.IsRegex) { target = mapping.Regex.Replace(input.TrimEnd(new char[] { '/' }), target); } if (mapping.PreserveQueryString) { target = string.Concat(target.TrimEnd(new char[] { '/' }), HttpContext.Current.Request.Url.Query); } if (!string.IsNullOrEmpty(Context.Site.VirtualFolder)) { target = string.Concat(StringUtil.EnsurePostfix('/', Context.Site.VirtualFolder), target.TrimStart(new char[] { '/' })); } return(target); }