Redirect URLs with a Trailing Slash
Warning: You are browsing the documentation for Symfony 3.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
The goal of this article is to demonstrate how to redirect URLs with a
trailing slash to the same URL without a trailing slash
(for example /en/blog/
to /en/blog
).
Create a controller that will match any URL with a trailing slash, remove the trailing slash (keeping query parameters if any) and redirect to the new URL with a 308 (HTTP Permanent Redirect) response status code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// src/AppBundle/Controller/RedirectingController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class RedirectingController extends Controller
{
public function removeTrailingSlashAction(Request $request)
{
$pathInfo = $request->getPathInfo();
$requestUri = $request->getRequestUri();
$url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri);
// 308 (Permanent Redirect) is similar to 301 (Moved Permanently) except
// that it does not allow changing the request method (e.g. from POST to GET)
return $this->redirect($url, 308);
}
}
After that, create a route to this controller that's matched whenever a URL with a trailing slash is requested. Be sure to put this route last in your system, as explained below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// src/AppBundle/Controller/RedirectingController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class RedirectingController extends Controller
{
/**
* @Route("/{url}", name="remove_trailing_slash",
* requirements={"url" = ".*\/$"})
*/
public function removeTrailingSlashAction(Request $request)
{
// ...
}
}
1 2 3 4 5
remove_trailing_slash:
path: /{url}
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
requirements:
url: .*/$
1 2 3 4 5 6 7
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing">
<route id="remove_trailing_slash" path="/{url}" methods="GET">
<default key="_controller">AppBundle:Redirecting:removeTrailingSlash</default>
<requirement key="url">.*/$</requirement>
</route>
</routes>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
$routes = new RouteCollection();
$routes->add(
'remove_trailing_slash',
new Route(
'/{url}',
[
'_controller' => 'AppBundle:Redirecting:removeTrailingSlash',
],
[
'url' => '.*/$',
]
)
);
Caution
Make sure to include this route in your routing configuration at the very end of your route listing. Otherwise, you risk redirecting real routes (including Symfony core routes) that actually do have a trailing slash in their path.