New in version 12.0.

This event is fired in the \TYPO3\CMS\Redirects\Http\Middleware\RedirectHandler middleware and allows extensions to further process the matched redirect and to adjust the PSR-7 response.


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.

Return type:PsrHttpMessageServerRequestInterface
Returns:The current PSR-7 Request
Return type:PsrHttpMessageUriInterface
Returns:the resolved redirect target
setMatchedRedirect(array matchedRedirect)

Set or update the matched redirect record

  • $matchedRedirect (array) – the matchedRedirect
Return type:array
Returns:the matched redirect record
setResponse(PsrHttpMessageResponseInterface response)

Set or update the PSR-7 Response

  • $response (PsrHttpMessageResponseInterface) – the response
Return type:PsrHttpMessageResponseInterface
Returns:the current PSR-7 Response

Example: Disable the hit count increment for monitoring tools

TYPO3 already implements the IncrementHitCount listener. It is used to increment the hit count of the matched redirect record, if the feature is enabled. In case you want to prevent the increment in some cases, for example when 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.

Registration of the event in the extensions’ Services.yaml:

    - name: event.listener
      identifier: 'my-package/redirects/validate-hit-count'
      before: 'redirects-increment-hit-count'

The corresponding event listener class:

use TYPO3\CMS\Redirects\Event\RedirectWasHitEvent;

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;

            // Also add a custom response header
                   'Hit count increment skipped')