3

What is a good strategy for handling 301 / 302 redirect maps when using Sitecore headless with SSR and the headless proxy?

My current thoughts are:

  • Need to handle SSR requests - either IIS redirect maps or SXA redirect maps (or a Sitecore redirect module) should be supported.
  • Need to handle layout service requests - I can't find any information on the approach here.

In both cases I need the proxy to pass on the redirect to the browser. Following the redirect and updating the route won't handle all scenarios and won't be good for seo.

3
  • It will be good to handle redirect rules at load balancer level to avoid any load on application level
    – Amit Kumar
    Commented Oct 27, 2021 at 17:04
  • @AmitKumar the approach you have mentioned will only take care of the very first bullet point. Commented Nov 3, 2021 at 22:17
  • For point#2, as per my understanding we have to extend Sitecore Http Request pipeline because it will hit the Sitecore to get the information. We can validate by overriding Sitecore Http Request pipeline.
    – Amit Kumar
    Commented Nov 4, 2021 at 2:01

1 Answer 1

8

For Next.JS and SXA

You can now use the new built-in functionality described here. See my blog post here for details on how to configure it.

For sitecore-jss-proxy:

After a lot of digging my findings are:

  1. The sitecore-jss-proxy package will always use the configured Layout Service to render pages.
  2. The sitecore-jss-proxy package does honour and support native redirects, in fact any response code from the layout service will be passed up to the browser.
  3. Therefore we can use our normal httpRequestBegin and/or mvc.requestBegin pipeline processors to do redirects on layout service requests.

You can get the item requested on the pipeline processor like this:

public static string GetLayoutServiceRequestItemPath()
{
    if (!HttpContext.Current.Request.Path.Contains("/layout/render"))
    {
        return null;
    }
    var item = HttpContext.Current.Request.QueryString?["item"];
    if (string.IsNullOrWhiteSpace(item))
    {
        return null;
    }
    return item;
}

And then apply whatever logic you need to resolve the item / read from a redirect map, and do your normal HttpContext.Current.Response.Redirect.

It is therefore simple to extend the Sitecore.XA.Feature.Redirects.Pipelines.HttpRequest.RedirectMapResolver for layout service requests by overriding the GetRawUrl() logic with the above.

This means that any other redirect module you use will need the above modification. It also means that IIS rewrite maps won't be supported without custom code to read and process the maps.

Finally, to round out the topic, you can do redirects on the node-proxy itself:

server.use((req, res, next) => {

  // SEO - remove trailing slashes
  if (req.path !== '/' && req.path.endsWith('/')) {
    res.redirect(301, req.path.substring(0, req.path.length - 1));
  }

  next();
})

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.