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

Add a cookbook entry about url trailing slash redirect #323

Merged
merged 1 commit into from
Nov 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cookbook/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions cookbook/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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`
76 changes: 76 additions & 0 deletions cookbook/redirect_urls_with_trailing_slash.rst
Original file line number Diff line number Diff line change
@@ -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);
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"And after that, register this controller to be executed whenever a url with a trailing slash is requested:"

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

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing">
<route id="remove_trailing_slash" path="/{url}">
<default key="_controller">AcmeDemoBundle:Redirecting:removeTrailingSlash</default>
<requirement key="url">.*/$</requirement>
<requirement key="_method">GET</requirement>
</route>
</routes>

.. 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',
)));
1 change: 1 addition & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down