For coders TYPO3 Tech Corner

[PHP] Avoid TYPO3 log entries like "The page is not configured! [type=...][]"

[PHP] Avoid TYPO3 log entries like "The page is not configured! [type=...][]"

If you often find entries in the log in the backend that can be traced back to the fact that some visitors to your website call up a TypeNum that does not exist, then we may have the right snippet for you.

Blog post updated on 2024-04-11

Do you know that too? You haven't had a TypeNum 98, 99, 100 or 3135 in your website configuration for a long time and yet there are still such requests on your site. These then lead to an error message and a status of 500. It's even worse when some hacking scripts clutter the TypeNum with SQL queries and this even migrates to the TYPO3 cache. Then the internal links look like this:

domain.org/page1/?type=UNION%20SELECT%20FROM%20..

If you do not want to block all of these types in the .htaccess, you can also hook early into the system via PSR-15 middleware and check if the requested type fits to the site configuration. In the example below, we then simply show the now found page with a status code of 404.

EXT:sitepackage/Configuration/RequestMiddlewares.php:

<?php return [ 'frontend' => [ 'sitepackage-undefinedtypenumerrorhandling' => [ 'target' => \In2code\In2template\Middleware\UndefinedTypeNumErrorHandling::class, 'before' => [ 'typo3/cms-redirects/redirecthandler' ], 'after' => [ 'typo3/cms-frontend/site', ] ] ] ];

EXT:sitepackage/Classes/Middleware/UndefinedTypeNumErrorHandling.php:

<?php declare(strict_types=1); namespace In2code\In2template\Middleware; use In2code\In2template\Exception\ConfigurationMissingException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Throwable; use TYPO3\CMS\Core\Site\Entity\Site; use TYPO3\CMS\Core\Utility\MathUtility; /** * Class UndefinedTypeNumErrorHandling * to simply show 404 page if a typeNum is given, that is not defined in siteconfiguration. * This avoids annoying log entries and delivers a 404 to the (e.g.) bot that is calling the outdated url. */ class UndefinedTypeNumErrorHandling implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { if ($this->isTypeNumSet($request) && $this->isTypeNumAllowed($request) === false) { /** @var Site $site */ $site = $request->getAttribute('site'); try { $errorHandler = $site->getErrorHandler(404); return $errorHandler->handlePageError($request, 'Given type is not registered in site configuration'); } catch (Throwable $exception) { throw new ConfigurationMissingException('No 404 error handler given in site configuration', 1643989065); } } return $handler->handle($request); } protected function isTypeNumAllowed(ServerRequestInterface $request): bool { /** @var Site $site */ $site = $request->getAttribute('site'); if ( $site !== null && is_a($site, NullSite::class) === false && !empty($site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map']) ) { $allowedTypeNums = array_values($site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map']); $type = $request->getQueryParams()['type']; if (MathUtility::canBeInterpretedAsInteger($type)) { return in_array((int)$type, $allowedTypeNums, true); } } return false; } }

Note: If you use this or a similar snippet, you have to make sure that all of your TypeNum definitions have also been included in the site configuration - e.g .:

rootPageId: 1 routes: - route: robots.txt type: staticText content: "Disallow: /typo3/\r\n" routeEnhancers: PageTypeSuffix: type: PageType default: / index: '' suffix: / map: print.html: 99 preview.html: 1560777975 newsletter.html: 1562349004 pixel.png: 1561894816

302 redirect instead of 404 page

If you want to have an example how to make a redirect instead of a 404 error:

<?php declare(strict_types=1); namespace In2code\In2template\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use TYPO3\CMS\Core\Http\RedirectResponse; use TYPO3\CMS\Core\Site\Entity\NullSite; use TYPO3\CMS\Core\Site\Entity\Site; use TYPO3\CMS\Core\Utility\MathUtility; /** * Class UndefinedTypeNumErrorHandling * to simply redirect to same page if a typeNum is given, that is not defined in site configuration. */ class UndefinedTypeNumErrorHandling implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { if ($this->isTypeNumSet($request) && $this->isTypeNumAllowed($request) === false) { return new RedirectResponse($this->getRedirectUri($request)); } return $handler->handle($request); } /** * Clone current URI and remove GET parameters and anchors * * @param ServerRequestInterface $request * @return UriInterface */ protected function getRedirectUri(ServerRequestInterface $request): UriInterface { $uri = (clone $request->getUri()) ->withQuery('') ->withScheme(''); return $uri; } protected function isTypeNumSet(ServerRequestInterface $request): bool { return array_key_exists('type', $request->getQueryParams()); } protected function isTypeNumAllowed(ServerRequestInterface $request): bool { /** @var Site $site */ $site = $request->getAttribute('site'); if ( $site !== null && is_a($site, NullSite::class) === false && !empty($site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map']) ) { $allowedTypeNums = array_values($site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map']); $type = $request->getQueryParams()['type']; if (MathUtility::canBeInterpretedAsInteger($type)) { return in_array((int)$type, $allowedTypeNums, true); } } return false; } }

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news

b13/container: Add and modify child elements in edit view

Unlike gridelements, you cannot manage the child elements in the B13 Container extension when you open the container in the editing view. I would be happy to show you how you can quickly install this...

Go to news

Menu comparison: Numbers, numbers, numbers

Go to news