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:
- The sitecore-jss-proxy package will always use the configured Layout Service to render pages.
- 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.
- 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();
})