From f410612aa25250573458679903344ca929a0ece6 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Tue, 7 Apr 2026 09:04:15 +1000 Subject: [PATCH] Add 301 redirect for site-prefixed URLs --- .../TideSiteRequestEventSubscriber.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/modules/tide_site/src/EventSubscriber/TideSiteRequestEventSubscriber.php b/modules/tide_site/src/EventSubscriber/TideSiteRequestEventSubscriber.php index 5e6e85488..3dee4ee9c 100644 --- a/modules/tide_site/src/EventSubscriber/TideSiteRequestEventSubscriber.php +++ b/modules/tide_site/src/EventSubscriber/TideSiteRequestEventSubscriber.php @@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; @@ -109,6 +110,16 @@ public function onRequestAddSiteFilter(RequestEvent $event) { $path = $api_helper->getRequestedPath($request); // Only prefix non-homepage and unrouted path. if ($path !== '/') { + // If the path already has a site prefix, 301 redirect to the clean + // path so the FE uses canonical URLs without site prefixes. + if ($this->helper->hasSitePrefix($path)) { + $clean_path = preg_replace('#^/site-\d+/#', '/', $path); + $query = $request->query->all(); + $query['path'] = $clean_path; + $redirect_url = $request->getBaseUrl() . $request->getPathInfo() . '?' . http_build_query($query); + $event->setResponse(new RedirectResponse($redirect_url, Response::HTTP_MOVED_PERMANENTLY)); + return; + } try { $url = Url::fromUri('internal:' . $path); if (!$url->isRouted() && !$this->helper->hasSitePrefix($path)) {