Пример #1
0
        protected virtual AdvancedRedirect FindMapping(string requestedPath)
        {
            AdvancedRedirect redirectMapping = null;

            List <AdvancedRedirect> .Enumerator enumerator = this.MappingsMap.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    AdvancedRedirect current = enumerator.Current;

                    //if the map entry key is regex
                    if (IsValidRegex(current.Source))
                    {
                        //if the current regex does not match the requested path, skip
                        if (!Regex.IsMatch(requestedPath, current.Source))
                        {
                            continue;
                        }
                    }
                    //if the map entry key path does not equal the request path, skip
                    else if (current.Source != requestedPath)
                    {
                        continue;
                    }

                    redirectMapping = current;
                    return(redirectMapping);
                }
            }
            finally
            {
                ((IDisposable)enumerator).Dispose();
            }
            return(redirectMapping);
        }
Пример #2
0
        public override void ProcessRequest(HttpRequestArgs args)
        {
            //Get the site redirect settings if they exist
            var siteLevel = SiteLevelConfigMapping;

            //if site redirect settings exist
            if (siteLevel != null)
            {
                var current = HttpContext.Current;

                if (current != null && siteLevel.RedirectToHttpsAlways)
                {
                    if (!current.Request.IsSecureConnection)
                    {
                        var url = current.Request.Url.AbsoluteUri;
                        if (url.StartsWith("http:"))
                        {
                            url = "https" + url.Remove(0, 4);
                            this.Redirect301(current.Response, url);
                        }
                    }
                }
                if (siteLevel.AddWwwPrefix)
                {
                    var url = current.Request.Url.AbsoluteUri;

                    var urlVal = url.Substring(url.IndexOf("//") + 2);
                    if (!urlVal.StartsWith("www."))
                    {
                        url = (url.Substring(0, url.IndexOf("//") + 2)) + "www." + urlVal;
                        this.Redirect301(current.Response, url);
                    }
                }
            }

            //get the requested path as a sitecore path relative to the lowest known item
            string path = Context.Request.FilePath;

            //check cache for previously resolved path
            AdvancedRedirect resolvedMapping = this.GetResolvedMapping(path);

            if (resolvedMapping == null)
            {
                //if not cached, generate mapping now
                resolvedMapping = this.FindMapping(path);
            }
            if (resolvedMapping != null)
            {
                var dictionaryitem = GetCache <Dictionary <string, AdvancedRedirect> >(ResolvedAdvancedRedirectPrefix)
                                     ?? new Dictionary <string, AdvancedRedirect>();

                dictionaryitem[path] = resolvedMapping;

                SetCache(ResolvedAdvancedRedirectPrefix, dictionaryitem);
            }
            if (resolvedMapping != null && HttpContext.Current != null)
            {
                if (!resolvedMapping.Target.IsNullOrEmpty())
                {
                    if (resolvedMapping.RedirectType == RedirectType.Redirect301)
                    {
                        this.Redirect301(HttpContext.Current.Response, resolvedMapping.Target);
                    }
                    else if (resolvedMapping.RedirectType == RedirectType.Redirect302)
                    {
                        HttpContext.Current.Response.Redirect(resolvedMapping.Target, true);
                    }
                    else if (resolvedMapping.RedirectType == RedirectType.ServerTransfer)
                    {
                        HttpContext.Current.Server.TransferRequest(resolvedMapping.Target);
                    }
                    //default to 302
                    else
                    {
                        HttpContext.Current.Response.Redirect(resolvedMapping.Target, true);
                    }

                    args.AbortPipeline();
                }
            }
        }