Skip to content

Add cookbook entry about url trailing slash redirect #3180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 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
Add cookbook entry about url trailing slash redirect
  • Loading branch information
EmmanuelVella committed Nov 15, 2013
commit e68d8d2aca68473b371fd495264b5451f8e4352e
1 change: 1 addition & 0 deletions cookbook/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
1 change: 1 addition & 0 deletions cookbook/routing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Routing
method_parameters
service_container_parameters
custom_route_loader
redirect_trailing_slash
90 changes: 90 additions & 0 deletions cookbook/routing/redirect_trailing_slash.rst
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

what if it is a post?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think that it does make much sense to force a GET request by redirecting an incoming POST request.

Copy link
Member

Choose a reason for hiding this comment

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

@cordoval Redirecting a POST request does not work well in old browsers. A 302 on a POST requests will send a GET request after the redirection for legacy reasons (it was handled like 303 since ages, but note that at least Firefox treats 302 with the original behavior in case of PUT), and the browser support for 307 (added with the original meaning of 302 to allow a proper support of redirecting POST without breaking BC) is limited

Copy link
Member

Choose a reason for hiding this comment

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

maybe a note about this can be usefull.

Thanks for the great information @stof !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof Thanks for the explanation ! I suppose the issue is the same for both 301 and 302 ?



.. 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',
)
)
);

.. 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.