diff --git a/cookbook/index.rst b/cookbook/index.rst index 91976698..3e46326c 100644 --- a/cookbook/index.rst +++ b/cookbook/index.rst @@ -13,5 +13,6 @@ The Cookbook creating_cms_using_cmf_and_sonata using_blockbundle_and_contentbundle handling_multilang_documents + redirect_urls_with_trailing_slash .. include:: map.rst.inc diff --git a/cookbook/map.rst.inc b/cookbook/map.rst.inc index 28e4b460..5de2cfa4 100644 --- a/cookbook/map.rst.inc +++ b/cookbook/map.rst.inc @@ -12,3 +12,4 @@ * :doc:`creating_cms_using_cmf_and_sonata` * :doc:`using_blockbundle_and_contentbundle` * :doc:`handling_multilang_documents` +* :doc:`redirect_urls_with_trailing_slash` diff --git a/cookbook/redirect_urls_with_trailing_slash.rst b/cookbook/redirect_urls_with_trailing_slash.rst new file mode 100644 index 00000000..ac7646d8 --- /dev/null +++ b/cookbook/redirect_urls_with_trailing_slash.rst @@ -0,0 +1,76 @@ +.. index:: + single: Redirect URLs with a trailing slash + +Redirect URLs with a trailing slash +=================================== + +The goal of this tutorial is to demonstrate how to redirect URLs with +trailing slash to the same url without trailing slash +(for example ``/en/blog/`` to ``/en/blog``). + +.. note:: + + For the moment, the :doc:`RoutingBundle <../bundles/routing/introduction>` + can't achieve this automatically. + +You have to create a controller which will match any URL with a trailing +slash, remove the trailing slash (keeping query parameters if any) and +redirect to 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', + ))); diff --git a/index.rst b/index.rst index ed889429..d6a6e379 100644 --- a/index.rst +++ b/index.rst @@ -143,6 +143,7 @@ Want to know more about the CMF and how each part can be configured? There's a t cookbook/handling_multilang_documents cookbook/installing_configuring_doctrine_phpcr_odm cookbook/using_blockbundle_and_contentbundle + cookbook/redirect_urls_with_trailing_slash Contributing ------------