Score:0

Fetch redirect alias using the old alias

ng flag

I want to get the current alias of the node using an old alias. I do have the Redirect module installed and all the old aliases of the nodes are mapped to the new ones. But I am not able to fetch the latest alias from the backend. Assume that my old url is "/old_url" and new url is "/new_url". I tried the following code but not able to get any output:

use Drupal\Core\Url;
use Drupal\redirect\RedirectRepository;

$path = "/old_url"
$url = Url::fromUri($path);
            
$redirect_repository = \Drupal::service('redirect.repository');
$redirect = $redirect_repository->findMatchingRedirect($url->toString());
           
$redirect_url = $redirect->getRedirectUrl()->toString();

$redirect_url should have the value "/new_url".

If this is not a right way to do this, how can I get this done?

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.