From 620ba175e457e367d5b4a05fe27b76e49c49bc06 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 2 Mar 2018 13:34:39 +0100 Subject: [PATCH 1/3] Documented the smart URL redirection --- routing.rst | 17 +++-- routing/redirect_trailing_slash.rst | 102 ++-------------------------- 2 files changed, 16 insertions(+), 103 deletions(-) diff --git a/routing.rst b/routing.rst index 39cf651e58c..9fcfa88e71a 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/``) then 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 version 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. From 4f57cddbde1250268f8e26df147740773339e30e Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sun, 4 Mar 2018 10:37:15 +0100 Subject: [PATCH 2/3] Fixed rst syntax --- routing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routing.rst b/routing.rst index 9fcfa88e71a..8b7e5870094 100644 --- a/routing.rst +++ b/routing.rst @@ -493,7 +493,7 @@ 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: +.. _routing-trailing-slash-redirection: Redirecting URLs with Trailing Slashes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 228bebe56db48fe6c78ebd58db672e7921a27b73 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sun, 4 Mar 2018 17:27:48 +0100 Subject: [PATCH 3/3] Minor fixes --- routing.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routing.rst b/routing.rst index 8b7e5870094..477f11c2aeb 100644 --- a/routing.rst +++ b/routing.rst @@ -517,12 +517,12 @@ Route path If the requested URL is ``/foo`` If the requested URL is `` .. note:: If your application defines different routes for each path (``/foo`` and - ``/foo/``) then this automatic redirection doesn't take place and the right + ``/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 version this results in a ``404`` response. + in Symfony 4.1. In previous Symfony versions this results in a ``404`` response. .. index:: single: Routing; Controllers