osm-labo/src/EventSubscriber/WikiRedirectSubscriber.php
2025-09-03 16:01:02 +02:00

51 lines
No EOL
1.7 KiB
PHP

<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;
class WikiRedirectSubscriber implements EventSubscriberInterface
{
private const WIKI_PATH_PREFIX = '/wiki';
private const REDIRECT_BASE_URL = 'https://qualiwiki.cipherbliss.com/wiki';
public function onKernelRequest(RequestEvent $event): void
{
// Don't do anything if it's not the master request
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
$path = $request->getPathInfo();
// Check if the path starts with /wiki
if (str_starts_with($path, self::WIKI_PATH_PREFIX)) {
// Extract the part after /wiki
$subPath = substr($path, strlen(self::WIKI_PATH_PREFIX));
// If subPath is empty or just a slash, redirect to the base URL
if (empty($subPath) || $subPath === '/') {
$redirectUrl = self::REDIRECT_BASE_URL;
} else {
// Otherwise, append the subPath to the redirect URL
$redirectUrl = self::REDIRECT_BASE_URL . $subPath;
}
// Create a redirect response
$response = new RedirectResponse($redirectUrl, 301); // 301 is permanent redirect
$event->setResponse($response);
}
}
public static function getSubscribedEvents(): array
{
// Use a high priority to intercept the request before the router
return [
KernelEvents::REQUEST => ['onKernelRequest', 256],
];
}
}