<?php
namespace App\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class SeoRequestSubscriber implements EventSubscriberInterface
{
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$uri = $request->getRequestUri();
$host = $request->getSchemeAndHttpHost();
if ($uri !== '/' && trim($uri, '/') === '') {
header("Location: {$host}", true, 301);
exit();
}
if (str_starts_with($uri, '/index.php')) {
$new = '/' . ltrim(str_replace('/index.php', '', $uri), '/');
header("Location: {$host}{$new}", true, 301);
exit();
}
$httpCache = $request->server->get('HTTP_IF_MODIFIED_SINCE', '');
if ($httpCache && time() + 3600 > strtotime($httpCache)) {
header($request->server->get('SERVER_PROTOCOL') . ' 304 Not Modified');
exit();
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}