I found a great way to do this in pure symfony (without apache mod_rewrite) without creating case insensitive forwarding rules for each route.
In this case, the ExceptionController control is used. Since this happens after the routing did not match (or 404 exception occurred for some other reason), it will not violate any existing routing URLs that use capital (although this would still be a bad idea).
namespace Application\Symfony\TwigBundle\Controller; use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController; class ExceptionController extends BaseExceptionController { public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) { if ( (string) $exception->getStatusCode() === '404' && preg_match('/[AZ]/', $request->getPathInfo())) { $lowercaseUrl = strtolower($request->getPathInfo()); if ($request->isMethod('POST')) { return new RedirectResponse( $lowercaseUrl, '307'
The only trick is that you need to configure this controller as a service in which you can enter the correct arguments into the constructor of the parent class:
in services.yml
services: application.custom.exception_controller: class: Application\Symfony\TwigBundle\Controller\ExceptionController arguments: [ "@twig", "%kernel.debug%" ]
in config.yml:
twig: exception_controller: application.custom.exception_controller:showAction
Of course, you can insert this controller and service definition anywhere
source share