From 854948858236b12de23be0c23f5c3bf12669fa73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sat, 17 Feb 2018 16:54:03 +0100 Subject: [PATCH 1/3] Use 308 to ensure http method is preserved The 307 and 308 status codes were introduced exactly to solve this issue. --- routing/redirect_trailing_slash.rst | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/routing/redirect_trailing_slash.rst b/routing/redirect_trailing_slash.rst index 1826cb7ba38..a391abc3f7f 100644 --- a/routing/redirect_trailing_slash.rst +++ b/routing/redirect_trailing_slash.rst @@ -10,7 +10,7 @@ trailing slash to the same URL without a trailing slash 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 301 response status code:: +new URL with a 308 response status code:: // src/AppBundle/Controller/RedirectingController.php namespace AppBundle\Controller; @@ -27,7 +27,7 @@ new URL with a 301 response status code:: $url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri); - return $this->redirect($url, 301); + return $this->redirect($url, 308); } } @@ -50,7 +50,7 @@ system, as explained below: { /** * @Route("/{url}", name="remove_trailing_slash", - * requirements={"url" = ".*\/$"}, methods={"GET"}) + * requirements={"url" = ".*\/$"}) */ public function removeTrailingSlashAction(Request $request) { @@ -65,7 +65,6 @@ system, as explained below: defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash } requirements: url: .*/$ - methods: [GET] .. code-block:: xml @@ -92,20 +91,10 @@ system, as explained below: ), array( 'url' => '.*/$', - ), - array(), - '', - array(), - array('GET') + ) ) ); -.. note:: - - Redirecting a POST request does not work well in old browsers. A 302 - on a POST request would send a GET request after the redirection for legacy - reasons. For that reason, the route here only matches GET requests. - .. caution:: Make sure to include this route in your routing configuration at the From f8f6bda2742310a1dc1d22484315df1f6d98147b Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sun, 18 Feb 2018 11:53:31 +0100 Subject: [PATCH 2/3] Added some minor explanation about 308 code --- routing/redirect_trailing_slash.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/routing/redirect_trailing_slash.rst b/routing/redirect_trailing_slash.rst index a391abc3f7f..7dfc60e0f1c 100644 --- a/routing/redirect_trailing_slash.rst +++ b/routing/redirect_trailing_slash.rst @@ -10,7 +10,7 @@ trailing slash to the same URL without a trailing slash 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 response status code:: +new URL with a 308 (*HTTP Permanent Redirect*) response status code:: // src/AppBundle/Controller/RedirectingController.php namespace AppBundle\Controller; @@ -27,6 +27,8 @@ new URL with a 308 response status code:: $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 from POST to GET return $this->redirect($url, 308); } } From e21cf2fdf2a2bc57b1bb27274e6fa519f7e2cf9c Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 20 Feb 2018 15:50:35 +0100 Subject: [PATCH 3/3] Minor change --- routing/redirect_trailing_slash.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routing/redirect_trailing_slash.rst b/routing/redirect_trailing_slash.rst index 7dfc60e0f1c..5233b535fff 100644 --- a/routing/redirect_trailing_slash.rst +++ b/routing/redirect_trailing_slash.rst @@ -28,7 +28,7 @@ new URL with a 308 (*HTTP Permanent Redirect*) response status code:: $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 from POST to GET + // that it does not allow changing the request method (e.g. from POST to GET) return $this->redirect($url, 308); } }