Example #1
0
        private void ContextOnEndRequest(object sender, EventArgs eventArgs)
        {
            HttpApplication application = (HttpApplication)sender;

            // Ignore if not a 404 response
            if (application.Response.StatusCode != 404)
            {
                return;
            }

            var request = application.Request;

            // Get the Umbraco domain of the current request
            Domain domain = GetUmbracoDomain(application.Request);

            // Get the root node/content ID of the domain (no domain = 0)
            int rootNodeId = domain?.ContentId ?? 0;

            // Look for a redirect matching the URL (and domain)
            RedirectItem redirect = null;

            if (rootNodeId > 0)
            {
                redirect = _redirects.GetRedirectByUrl(rootNodeId, application.Request.RawUrl);
            }
            redirect = redirect ?? _redirects.GetRedirectByUrl(0, application.Request.RawUrl);
            if (redirect == null)
            {
                return;
            }

            var redirectUrl = redirect.LinkUrl;

            if (redirect.ForwardQueryString)
            {
                redirectUrl = _redirects.HandleForwardQueryString(redirect, application.Request.RawUrl);
            }

            //if (redirect.IsRegex)
            //{
            //    var regex = new Regex(redirect.Url);

            //    if (_capturingGroupsRegex.IsMatch(redirectUrl))
            //    {
            //        redirectUrl = regex.Replace(redirect.Url, redirectUrl);
            //    }
            //}

            // Redirect to the URL
            if (redirect.IsPermanent)
            {
                application.Response.RedirectPermanent(redirectUrl);
            }
            else
            {
                application.Response.Redirect(redirectUrl);
            }
        }