next-drupal
next-drupal copied to clipboard
How to redirect node/NID to the node alias?
I am looking for a way to do this, I am already handling redirects but this is more custom. I am seeing that node/NID is not part of the response of translatePathFromContext(), I might build the slug myself and compare it to path.entity.path but I am thinking this is a bit precarious.
Any advise?
This seems to be working, but still I wonder if there's something more integrated into decoupled router or next.
// Redirect to an alias.
const slug = drupal.getPathFromContext(context);
if (path.entity?.path) {
if (path.entity.path != slug) {
return {
redirect: {
destination: path.entity.path,
permanent: true,
},
}
}
}
@hanoii there is this part here in the documentation on the Redirects module topic – https://next-drupal.org/guides/redirects#redirect-module
Closing as resolved since a documented solution was provided.