Geta / 404handler

The popular 404 handler for EPiServer, enabling better control over your 404 page in addition to allowing redirects for old urls that no longer works.
Apache License 2.0
54 stars 51 forks source link

Not hitting the INotFoundHandler implementation #98

Closed Kuubs closed 6 years ago

Kuubs commented 6 years ago

At this moment the requests to unexisting URLs do not hit the configured INotFoundHandler implementation. Instead they immediately hit the ErrorHandlerController and I can not explain why. Locally when debugging I do hit themethod RewriteUrl in the INotFoundHandler implemantation. But externally, on Test (azure) the implantation is not hit. We tested this by adding an Exception on test.

Can you please assist me finding the problem? Below you will find the web.config file and the implementation of INotFoundHandler. Thanks in advance!

Web.config:

  <configSections>
    <section name="bvn404Handler" type="BVNetwork.NotFound.Configuration.Bvn404HandlerConfiguration, BVNetwork.EPi404" />

  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace" xdt:Transform="Insert">
      <remove statusCode="404" />
      <error statusCode="404" path="/ErrorHandler/Error404" responseMode="ExecuteURL" />
    </httpErrors>

  <bvn404Handler handlerMode="On" logging="Off" ignoredResourceExtensions="jpg,gif,png,css,js,ico,swf,woff,eot,otf">
    <providers>
      <add name="VariantRedirectHandler" type="Valtech.BeterBed.Web.Infrastructure.Redirects.VariantRedirectHandler, Valtech.BeterBed.Web, Version=1.0.0.0, Culture=neutral" />
    </providers>
  </bvn404Handler>

Implementation of the not found handler:

public class VariantRedirectHandler : INotFoundHandler
    {
        public string RewriteUrl(string url)
        {
            Url uri = new Url(url);

            if (Regex.IsMatch(uri.ToString(), @"\/producten\/[\d]{6}", RegexOptions.IgnoreCase))
            {
                var variationCode = Regex.Match(uri.ToString(), @"[\d]{6}", RegexOptions.IgnoreCase).Value;

                if (!string.IsNullOrEmpty(variationCode))
                {
                    HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Redirect;
                    var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
                    var variantLink = referenceConverter.GetContentLink(variationCode);

                    if (variantLink != ContentReference.EmptyReference)
                    {
                        var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
                        var variant = repo.Get<DefaultVariation>(variantLink);
                        var redirectTo = variant?.GetUrl();
                        HttpContext.Current.Response.Redirect(redirectTo);
                        HttpContext.Current.Response.End();
                    }
                }
            }

            return null;
        }
    }
marisks commented 6 years ago

It might not hit the handler if there is already other redirects defined. Then provider scanning is skipped.

P.S There is an issue in your code - the handler should return URL to redirect instead of redirecting yourself.

marisks commented 6 years ago

Were you able to figure out why it doesn't work for you?

marisks commented 6 years ago

Closing for now. If you still have this issue, feel free to open the issue again.