next-drupal icon indicating copy to clipboard operation
next-drupal copied to clipboard

How to redirect node/NID to the node alias?

Open hanoii opened this issue 2 years ago • 2 comments

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?

hanoii avatar Apr 18 '23 14:04 hanoii

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 avatar Apr 18 '23 14:04 hanoii

@hanoii there is this part here in the documentation on the Redirects module topic – https://next-drupal.org/guides/redirects#redirect-module

yannickoo avatar Aug 29 '23 21:08 yannickoo

Closing as resolved since a documented solution was provided.

backlineint avatar Jun 27 '24 19:06 backlineint