From e68d8d2aca68473b371fd495264b5451f8e4352e Mon Sep 17 00:00:00 2001 From: Emmanuel Vella Date: Wed, 13 Nov 2013 09:42:55 +0100 Subject: [PATCH] Add cookbook entry about url trailing slash redirect --- cookbook/map.rst.inc | 1 + cookbook/routing/index.rst | 1 + cookbook/routing/redirect_trailing_slash.rst | 90 ++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 cookbook/routing/redirect_trailing_slash.rst diff --git a/cookbook/map.rst.inc b/cookbook/map.rst.inc index e5405c98cac..ece47d25be9 100644 --- a/cookbook/map.rst.inc +++ b/cookbook/map.rst.inc @@ -115,6 +115,7 @@ * :doc:`/cookbook/routing/method_parameters` * :doc:`/cookbook/routing/service_container_parameters` * :doc:`/cookbook/routing/custom_route_loader` + * :doc:`/cookbook/routing/redirect_trailing_slash` * :doc:`/cookbook/security/index` diff --git a/cookbook/routing/index.rst b/cookbook/routing/index.rst index 64bb70e54b0..526839e77c5 100644 --- a/cookbook/routing/index.rst +++ b/cookbook/routing/index.rst @@ -10,3 +10,4 @@ Routing method_parameters service_container_parameters custom_route_loader + redirect_trailing_slash diff --git a/cookbook/routing/redirect_trailing_slash.rst b/cookbook/routing/redirect_trailing_slash.rst new file mode 100644 index 00000000000..6f32a5932aa --- /dev/null +++ b/cookbook/routing/redirect_trailing_slash.rst @@ -0,0 +1,90 @@ +.. index:: + single: Routing; Redirect URLs with a trailing slash + +Redirect URLs with a Trailing Slash +=================================== + +The goal of this cookbook is to demonstrate how to redirect URLs with +trailing slash to the same URL without a trailing slash +(for example ``/en/blog/`` to ``/en/blog``). + +You have to 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:: + + // src/Acme/DemoBundle/Controller/RedirectingController.php + namespace Acme\DemoBundle\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); + + return $this->redirect($url, 301); + } + } + +And after that, register this controller to be executed whenever a URL +with a trailing slash is requested: + +.. configuration-block:: + + .. code-block:: yaml + + remove_trailing_slash: + path: /{url} + defaults: { _controller: AcmeDemoBundle:Redirecting:removeTrailingSlash } + requirements: + url: .*/$ + _method: GET + + + .. code-block:: xml + + + + + AcmeDemoBundle:Redirecting:removeTrailingSlash + .*/$ + GET + + + + .. code-block:: php + + use Symfony\Component\Routing\RouteCollection; + use Symfony\Component\Routing\Route; + + $collection = new RouteCollection(); + $collection->add( + 'remove_trailing_slash', + new Route( + '/{url}', + array( + '_controller' => 'AcmeDemoBundle:Redirecting:removeTrailingSlash', + ), + array( + 'url' => '.*/$', + '_method' => 'GET', + ) + ) + ); + +.. note:: + + Redirecting a POST request does not work well in old browsers. + A 302 on a POST request will send a GET request after the + redirection for legacy reasons. + +.. caution:: + + Make sure to include this route in your routing configuration at + the very end of your route listing. Otherwise, you risk to redirect + Symfony2 core routes that natively do have a trailing slash.