2

Our Sitecore application (v10.1.3) is hosted on Sitecore Managed Cloud. The following is the reason for implementing redirection for page requests.

The website's original domain/URL was https://example-toy-store.com/.
Later, it was decided that the site should only be browsed by https://example.com/toy.
example.com is a non-sitecore website hosted somewhere else and the vendor handling that domain has implemented a reverse proxy (RP) solution.

We have added a virtual folder /toy in the Sitecore application. When the user browses https://example.com/toy/*, the RP implementation will fetch the content from https://example-toy-store.com/* and render on page, without changing the URL in browser. At the Sitecore application side, we need to redirect the requests for https://example-toy-store.com/* to https://example.com/toy/*

When we added a redirection rule in Sitecore application's web.config, for the above old-to-new-site usecase, this resulted in an infinite loop situation.

And that is the reason, we had to handle this in code.

The logic is currently in the preProcessRequest like this:

<sitecore>
 <pipelines>
    <preprocessRequest>
        <processor role:require="Standalone or ContentDelivery" type="MySite.Foundation.Multisite.Pipelines.RedirectOldDomainRequest, MySite.Foundation.Multisite" patch:before="processor[@type='Sitecore.Pipelines.PreprocessRequest.IIS404Handler, Sitecore.Kernel']" />
   </preprocessRequest>
 </pipelines>
</sitecore>

The implementation works fine with the redirects but we have observed a huge number of 404 failure requests in the Azure App Insights. Example GET /404 for https://www.example-toy-store.com/404?item=%2Fbattery&user=extranet%5CAnonymous&site=website

But the /battery is a valid page item.

So, is the current redirection class placed in the correct pipeline or should it be moved to httpBeginRequest instead.

Thank you

3
  • Can you also please share the code for the RedirectOldDomainRequest processor in order to best suggest where that processor should be placed inside the httprequestbegin pipeline.
    – ckhanna
    Commented Sep 18 at 8:58
  • Why are you customizing pipelines for this. It seems like it would be much easier to achieve this by adding redirects/rewrites in the web.config and letting IIS handle the redirect.
    – Richard Seal
    Commented Sep 18 at 13:19
  • @RichardSeal We did and it ended in infinite loops, because one redirection is to redirect it to the RP server. But RP server is also sending request to old domain. That's why the redirection had to be done on conditional logic and so we are handling it in code.
    – sukesh
    Commented Sep 18 at 13:24

3 Answers 3

1

Your redirection logic, which is currently placed in the preProcessRequest pipeline, is one of the earliest stages of the request handling in Sitecore. However, placing the redirection there might cause the kind of issue you're seeing—specifically, it may interfere with Sitecore's standard request processing and lead to 404 errors, even for valid items.

Reason for the Issue:

The preProcessRequest pipeline runs before Sitecore has fully determined whether an item exists. This can result in the redirection happening too early, potentially causing valid pages to return a 404 if the redirection logic triggers prematurely.

Suggested Solution:

It would be more appropriate to move your redirection logic to the httpBeginRequest pipeline. This stage occurs later in the request lifecycle, after Sitecore has had a chance to resolve the item and validate the requested content. By placing the redirection here, you can ensure that valid pages are properly processed before any redirection logic kicks in.

Updated Configuration:

<sitecore>
  <pipelines>
    <httpRequestBegin>
      <processor role:require="Standalone or ContentDelivery" type="MySite.Foundation.Multisite.Pipelines.RedirectOldDomainRequest, MySite.Foundation.Multisite" patch:before="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" />
    </httpRequestBegin>
  </pipelines>
</sitecore>

Key Points:

  • Pipeline Change: Move your redirect logic from preProcessRequest to httpBeginRequest.
  • 404 Issue: The 404 errors you're seeing may be due to the redirection being processed before Sitecore resolves the valid page items. This should be resolved by moving to a later pipeline.
  • Reverse Proxy: Since you're using a reverse proxy, this change will ensure that valid content is rendered properly while maintaining the redirection logic.

Let me know if this resolves the issue!

1

The preProcessRequest pipeline typically handles incoming requests before Sitecore processes them. While it's often suitable for implementing redirection logic, it may not be the best solution for you, particularly given that you're encountering 404 errors for pages that should be valid.

Move to httpBeginRequest Pipeline: It might be better to use the httpBeginRequest pipeline for your processor. This happens earlier when a web request comes in, so you can redirect before Sitecore starts looking for the item. This way, you can stop Sitecore from processing requests for real items that should be sent somewhere else instead.

<sitecore>
    <pipelines>
        <httpBeginRequest>
            <processor role:require="Standalone or ContentDelivery" type="MySite.Foundation.Multisite.Pipelines.RedirectOldDomainRequest, MySite.Foundation.Multisite" />
        </httpBeginRequest>
    </pipelines>
</sitecore>

Before sending the user to a new web address, check if they're already on the right page. This stops the website from getting stuck in an endless loop of redirects.

if (HttpContext.Current.Request.Url.Host.Equals("example-toy-store.com", StringComparison.OrdinalIgnoreCase))
{
    // Redirect logic
    var newUrl = $"https://example.com/toy/{HttpContext.Current.Request.Path}";
    HttpContext.Current.Response.Redirect(newUrl, true);
}

Hope this helps!!!

1

You camn move the redirection logic from preProcessRequest pipeline to httpRequestBegin pipeline. Inside the pipeline processor when you are redirecting make sure you are aborting the processor as below.

public class WebsiteRedirection : HttpRequestProcessor
{
   public override void Process(HttpRequestArgs args)
   {
      if (args.HttpContext.Request.Url.Host.Contains("example-toy-store.com"))
      {
          string redirectPath = $"https://example.com/toy{args.HttpContext.Request.Path}";
          args.AbortPipeline();
          args.HttpContext.Response.ClearHeaders();
          args.HttpContext.Response.Redirect(redirectPath,true);
          args.HttpContext.Response.End();
      }
   }
}

This will make sure the other processors below the pipeline does not gets triggered and you will not face the 404 issues you are currently getting. The main reason you are getting path like this 404?item=%2Fbattery&user=extranet%5CAnonymous&site=website is because of the ExecuteRequest processor getting triggered inside the httpRequestBegin pipeline, which should not be get called when the request contains example-toy-store.com domain.

Update:

You can patch this processor in the httpRequestBegin pipeline as below.

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
   <sitecore>
     <pipelines>
        <httpRequestBegin>
           <processor type="SitecoreCustomExtensions.Pipelines.WebsiteRedirection, SitecoreCustomExtensions"
                        patch:before="*[@type='Sitecore.Pipelines.HttpRequest.IgnoreList, Sitecore.Kernel']" />
         </httpRequestBegin>
       </pipelines>
   </sitecore>
</configuration>

Hope this helps!!! Let me know in case you have any queries.

5
  • Would this be a better approach than moving the pipeline to httpRequestBegin?
    – sukesh
    Commented Sep 18 at 13:22
  • 1
    ya I dont see any reason of moving it to httpRequestBegin pipeline because this will be getting triggered only after preprocessrequest pipeline. If we can abort the execution at the first level only, then the second level wont get even triggered and can prevent the issue you are getting..Hope that makes sense!!!
    – ckhanna
    Commented Sep 18 at 13:27
  • @sukesh I just updated the answer below where the pipline to be placed in the httpRequestBegin pipeline to avoid the issue you are getting. Let me know in case you have any queries.
    – ckhanna
    Commented Sep 19 at 7:02
  • Should the custom code be patched before Sitecore.Pipelines.HttpRequest.IgnoreList or Sitecore.Pipelines.HttpRequest.ItemResolver
    – sukesh
    Commented Sep 19 at 7:57
  • @sukesh Since you are redirecting right, so this can be done at the initial stage rather than doing in the later stages since you do want those processors to be executed once the redirection has happened...
    – ckhanna
    Commented Sep 19 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.