Issue Feature Closed

#102365Regular expression with page id target

Bernhard Eckl 11/13/23, 09:07 updated 10/07/26, 20:30

Description

When using a regular expression where the end of the url should be added to the target url, it is only possible via target external url but not with page id and adding $1 to the url.

E.g. the example here https://docs.typo3.org/c/typo3/cms-redirects/main/en-us/Usage/Index.html has target https://example.org/newpath/$1 but it would be great to have a target url like t3://page?uid=196/$1.

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

History 4

  1. Stefan Bürk commented and changed assigned to Stefan Bürk 01/16/24, 01:59

    t3://page?uid=196/$1

    does not make much sense – adding a “path” after starging with query arguments is not valid url syntax at all.

    Please provide a full description of your redirect record created with all fields, at best using the TYPO3
    monorepo along with the `ext:styleguide` – which can be used than to create a test first before trying to
    change the implementation.

    On the other hand, it would make clear what you want to pass to the target.

    Additional, please mention TYPO3 version(s) ;)

  2. Stefan Bürk changed status New Needs Feedback 01/16/24, 01:59

  3. Bernhard Eckl commented 01/16/24, 17:12

    What I mean is the following:

    In nginx it would be:
    rewrite ^/foo/(.*)$ https://domain.tld/bar/$1 permanent;

    If the user calls an url like
    https://domain.tld/foo/anotherdirectory
    it would redirect to
    https://domain.tld/bar/anotherdirectory

    But I would like to redirect via TYPO3 because somebody could change the slug of the target page.
    E.g.:
    - Source path: /foo/(.*)$
    - Target: t3://page?uid=196/$1
    (page id 196 has slug called bar)

    In other words: add a part of the source path to the target path but also use a page id in the target path.
    Is it now more clean what I mean?

    TYPO3 version: 11.5.33

  4. Georg Ringer commented and changed status Needs Feedback Closed 10/07/26, 20:30

    Thanks for the detailed explanation, Bernhard – the use case is clear now.

    However, I’d suggest not to implement this in the core. A page target identifies exactly one page, while /bar/$1 points to a URL that only exists if the subpages of the target page happen to mirror the old structure. The core can’t know or validate that target, so things like the redirect integrity check or the loop detection in the RedirectHandler would have to deal with targets they can’t verify. Adding a dedicated field (e.g. a “path suffix” for regexp redirects) including TCA, database schema and documentation seems too much for such a specific case.

    This can already be solved in a project without touching the core:

    Option 1: Listener for RedirectWasHitEvent

    The event is dispatched after the target URL has been built and allows to replace the response. It provides the matched redirect record, the request and the generated target URL:

    use TYPO3\CMS\Core\Attribute\AsEventListener;
    use TYPO3\CMS\Redirects\Event\RedirectWasHitEvent;
    
    final readonly class AppendCaptureGroupToPageRedirect
    {
        #[AsEventListener]
        public function __invoke(RedirectWasHitEvent $event): void
        {
            $redirect = $event->getMatchedRedirect();
            if (!($redirect['is_regexp'] ?? false)
                || !str_starts_with((string)$redirect['target'], 't3://page')
            ) {
                return;
            }
            $path = rawurldecode($event->getRequest()->getUri()->getPath());
            if (!@preg_match($redirect['source_path'], $path, $matches)
                || ($matches[1] ?? '') === ''
            ) {
                return;
            }
            $suffix = explode('?', $matches[1], 2)[0];
            $targetUrl = $event->getTargetUrl();
            $targetUrl = $targetUrl->withPath(
                rtrim($targetUrl->getPath(), '/') . '/' . ltrim($suffix, '/')
            );
            $event->setResponse(
                $event->getResponse()->withHeader('location', (string)$targetUrl)
            );
        }
    }

    You probably want to limit this to selected redirects (e.g. by uid or a custom field), as it would otherwise apply to every regexp redirect with a page target.

    Option 2: Custom middleware

    If you need full control (e.g. own matching rules, a separate configuration source or a status code logic), a custom PSR-15 middleware placed before typo3/cms-redirects/redirecthandler in Configuration/RequestMiddlewares.php can handle these requests completely on its own and return the redirect response directly. Inside, the target page URL can be generated with the site router, e.g. $site->getRouter()->generateUri($pageId), so slug changes of the target page are still respected.

    Both ways keep the “slug of the target page may change” benefit you are looking for, without the need for a new core feature.