diff --git a/routing.rst b/routing.rst index 39cf651e58c..477f11c2aeb 100644 --- a/routing.rst +++ b/routing.rst @@ -493,6 +493,8 @@ that are special: each adds a unique piece of functionality inside your applicat ``_locale`` Used to set the locale on the request (:ref:`read more `). +.. _routing-trailing-slash-redirection: + Redirecting URLs with Trailing Slashes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -508,14 +510,19 @@ slashes (but only for ``GET`` and ``HEAD`` requests): ---------- ---------------------------------------- ------------------------------------------ Route path If the requested URL is ``/foo`` If the requested URL is ``/foo/`` ---------- ---------------------------------------- ------------------------------------------ -``/foo`` It matches (``200`` status response) It doesn't match (``404`` status response) +``/foo`` It matches (``200`` status response) It makes a ``301`` redirect to ``/foo`` ``/foo/`` It makes a ``301`` redirect to ``/foo/`` It matches (``200`` status response) ---------- ---------------------------------------- ------------------------------------------ -In summary, adding a trailing slash in the route path is the best way to ensure -that both URLs work. Read the :doc:`/routing/redirect_trailing_slash` article to -learn how to avoid the ``404`` error when the request URL contains a trailing -slash and the route path does not. +.. note:: + + If your application defines different routes for each path (``/foo`` and + ``/foo/``) this automatic redirection doesn't take place and the right + route is always matched. + +.. versionadded:: 4.1 + The automatic ``301`` redirection from ``/foo/`` to ``/foo`` was introduced + in Symfony 4.1. In previous Symfony versions this results in a ``404`` response. .. index:: single: Routing; Controllers diff --git a/routing/redirect_trailing_slash.rst b/routing/redirect_trailing_slash.rst index 5d23ea68fb9..9f17a8c0b07 100644 --- a/routing/redirect_trailing_slash.rst +++ b/routing/redirect_trailing_slash.rst @@ -4,103 +4,9 @@ Redirect URLs with a Trailing Slash =================================== -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:: - - // src/Controller/RedirectingController.php - namespace App\Controller; - - use Symfony\Bundle\FrameworkBundle\Controller\Controller; - use Symfony\Component\HttpFoundation\Request; - - class RedirectingController extends Controller - { - public function removeTrailingSlash(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: - -.. configuration-block:: - - .. code-block:: php-annotations - - // src/Controller/RedirectingController.php - namespace App\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 removeTrailingSlash(Request $request) - { - // ... - } - } - - .. code-block:: yaml - - # config/routes.yaml - remove_trailing_slash: - path: /{url} - controller: App\Controller\RedirectingController::removeTrailingSlash - requirements: - url: .*/$ - - .. code-block:: xml - - - - - App\Controller\RedirectingController::removeTrailingSlash - .*/$ - - - - .. code-block:: php - - use Symfony\Component\Routing\RouteCollection; - use Symfony\Component\Routing\Route; - - $routes = new RouteCollection(); - $routes->add( - 'remove_trailing_slash', - new Route( - '/{url}', - array( - '_controller' => 'App\Controller\RedirectingController::removeTrailingSlash', - ), - array( - '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. + In Symfony 4.1 the automatic URL redirection was improved as explained in + :ref:`routing-trailing-slash-redirection`. That's why you no longer need to + do that redirection yourself and this article has been removed because it's + no longer needed.