Redirect for path with trailing slash is not searched

Created on 11 March 2024, about 1 year ago
Updated 24 March 2024, about 1 year ago

Problem/Motivation

Decoupled router is not able to search for path's redirect if queried with a trailing slash. So, /router/translate-path?path=/test-page resolves to related redirect but /router/translate-path?path=/test-page/ shows message - "Unable to resolve path /test-page/."

Steps to reproduce

1. Create a page with alias "/test-page".
2. Add a redirect from "/test-page" to some other page.
3. Check response of decoupled router in below cases
Without trailing slash: /router/translate-path?path=/test-page
- A redirect section is added to the response.
With trailing slash: /router/translate-path?path=/test-page/
- No data in response with message "Unable to resolve path /test-page/."

Expected: In both cases there should be a response along with redirect data.

Proposed resolution

In RedirectPathTranslatorSubscriber, redirects are being searched without trimming trailing slash from path but redirect module stores redirect path without leading and trailing slashes.
So, updating as below in RedirectPathTranslatorSubscriber fixes this issue.

--- a/src/EventSubscriber/RedirectPathTranslatorSubscriber.php
+++ b/src/EventSubscriber/RedirectPathTranslatorSubscriber.php
@@ -50,8 +50,8 @@ class RedirectPathTranslatorSubscriber extends RouterPathTranslatorSubscriber {
       $results = $redirect_storage
         ->getQuery()
         ->accessCheck(TRUE)
-        // Redirects are stored without the leading slash :-(.
-        ->condition('redirect_source.path', ltrim($destination, '/'))
+        // Redirects are stored without the leading and trailing slash :-(.
+        ->condition('redirect_source.path', trim($destination, '/'))
         ->execute();
🐛 Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

🇮🇳India sumit_saini

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024