RedirectWasHitEvent

New in version 12.0

The PSR-14 event \TYPO3\CMS\Redirects\Event\RedirectWasHitEvent is fired in the \TYPO3\CMS\Redirects\Http\Middleware\RedirectHandlermiddleware and allows extension authors to further process the matched redirect and to adjust the PSR-7 response.

Example: Disable the hit count increment for monitoring tools

TYPO3 already implements the EXT:redirects/Classes/EventListener/IncrementHitCount.php (GitHub) listener. It is used to increment the hit count of the matching redirect record, if the feature "redirects.hitCount" is enabled. In case you want to prevent the increment in some cases, for example if the request was initiated by a monitoring tool, you can either implement your own listener with the same identifier (redirects-increment-hit-count) or add your custom listener before and dynamically set the records disable_hitcount flag.

EXT:my_extension/Classes/Redirects/EventListener/MyEventListener.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Redirects\EventListener;

use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Redirects\Event\RedirectWasHitEvent;

#[AsEventListener(
    identifier: 'my-extension/redirects/validate-hit-count',
    before: 'redirects-increment-hit-count',
)]
final readonly class MyEventListener
{
    public function __invoke(RedirectWasHitEvent $event): void
    {
        $matchedRedirect = $event->getMatchedRedirect();

        // This will disable the hit count increment in case the target
        // is the page 123 and the request is from the monitoring tool.
        if (str_contains($matchedRedirect['target'], 'uid=123')
            && $event->getRequest()->getAttribute('normalizedParams')
                ->getHttpUserAgent() === 'my monitoring tool'
        ) {
            $matchedRedirect['disable_hitcount'] = true;
            $event->setMatchedRedirect(
                $matchedRedirect,
            );

            // Also add a custom response header
            $event->setResponse(
                $event->getResponse()->withAddedHeader(
                    'X-My-Custom-Header',
                    'Hit count increment skipped',
                ),
            );
        }
    }
}
Copied!

New in version 13.0

The PHP attribute \TYPO3\CMS\Core\Attribute\AsEventListener has been introduced to tag a PHP class as an event listener. Alternatively, or if you need to be compatible with older TYPO3 versions, you can also register an event listener via the Configuration/Services.yaml file. Switch to an older version of this page for an example or have a look at the section Implementing an event listener in your extension.

API

class \TYPO3\CMS\Redirects\Event\ RedirectWasHitEvent

This event is fired in the TYPO3CMSRedirectsHttpMiddlewareRedirectHandler middleware when a request matches a configured redirect.

It can be used to further process the matched redirect and to adjust the PSR-7 Response. It furthermore allows to influence Core functionality, for example the hit count increment.

getRequest ( )
returntype

Psr\Http\Message\ServerRequestInterface

getTargetUrl ( )
returntype

Psr\Http\Message\UriInterface

setMatchedRedirect ( array $matchedRedirect)
param array $matchedRedirect

the matchedRedirect

getMatchedRedirect ( )
returntype

array

setResponse ( Psr\\Http\\Message\\ResponseInterface $response)
param Psr\\Http\\Message\\ResponseInterface $response

the response

getResponse ( )
returntype

Psr\Http\Message\ResponseInterface