Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 629497b

Browse files
committed
Merge pull request #323 from EmmanuelVella/trailing-slash-cookbook
Add a cookbook entry about url trailing slash redirect
2 parents 5646b6e + 1a7d8e8 commit 629497b

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

cookbook/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ The Cookbook
1313
creating_cms_using_cmf_and_sonata
1414
using_blockbundle_and_contentbundle
1515
handling_multilang_documents
16+
redirect_urls_with_trailing_slash
1617

1718
.. include:: map.rst.inc

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
* :doc:`creating_cms_using_cmf_and_sonata`
1313
* :doc:`using_blockbundle_and_contentbundle`
1414
* :doc:`handling_multilang_documents`
15+
* :doc:`redirect_urls_with_trailing_slash`
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. index::
2+
single: Redirect URLs with a trailing slash
3+
4+
Redirect URLs with a trailing slash
5+
===================================
6+
7+
The goal of this tutorial is to demonstrate how to redirect URLs with
8+
trailing slash to the same url without trailing slash
9+
(for example ``/en/blog/`` to ``/en/blog``).
10+
11+
.. note::
12+
13+
For the moment, the :doc:`RoutingBundle <../bundles/routing/introduction>`
14+
can't achieve this automatically.
15+
16+
You have to create a controller which will match any URL with a trailing
17+
slash, remove the trailing slash (keeping query parameters if any) and
18+
redirect to new URL with a 301 response status code::
19+
20+
// src/Acme/DemoBundle/Controller/RedirectingController.php
21+
namespace Acme\DemoBundle\Controller;
22+
23+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
24+
use Symfony\Component\HttpFoundation\Request;
25+
26+
class RedirectingController extends Controller
27+
{
28+
public function removeTrailingSlashAction(Request $request)
29+
{
30+
$pathInfo = $request->getPathInfo();
31+
$requestUri = $request->getRequestUri();
32+
33+
$url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri);
34+
35+
return $this->redirect($url, 301);
36+
}
37+
}
38+
39+
And after that, register this controller to be executed whenever a url
40+
with a trailing slash is requested:
41+
42+
.. configuration-block::
43+
44+
.. code-block:: yaml
45+
46+
remove_trailing_slash:
47+
path: /{url}
48+
defaults: { _controller: AcmeDemoBundle:Redirecting:removeTrailingSlash }
49+
requirements:
50+
url: .*/$
51+
_method: GET
52+
53+
54+
.. code-block:: xml
55+
56+
<?xml version="1.0" encoding="UTF-8" ?>
57+
<routes xmlns="http://symfony.com/schema/routing">
58+
<route id="remove_trailing_slash" path="/{url}">
59+
<default key="_controller">AcmeDemoBundle:Redirecting:removeTrailingSlash</default>
60+
<requirement key="url">.*/$</requirement>
61+
<requirement key="_method">GET</requirement>
62+
</route>
63+
</routes>
64+
65+
.. code-block:: php
66+
67+
use Symfony\Component\Routing\RouteCollection;
68+
use Symfony\Component\Routing\Route;
69+
70+
$collection = new RouteCollection();
71+
$collection->add('remove_trailing_slash', new Route('/{url}', array(
72+
'_controller' => 'AcmeDemoBundle:Redirecting:removeTrailingSlash',
73+
), array(
74+
'url' => '.*/$',
75+
'_method' => 'GET',
76+
)));

index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Want to know more about the CMF and how each part can be configured? There's a t
143143
cookbook/handling_multilang_documents
144144
cookbook/installing_configuring_doctrine_phpcr_odm
145145
cookbook/using_blockbundle_and_contentbundle
146+
cookbook/redirect_urls_with_trailing_slash
146147

147148
Contributing
148149
------------

0 commit comments

Comments
 (0)