10000 Documented the smart URL redirection by javiereguiluz · Pull Request #9385 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Documented the smart URL redirection #9385

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Documented the smart URL redirection
  • Loading branch information
javiereguiluz committed Mar 2, 2018
commit 620ba175e457e367d5b4a05fe27b76e49c49bc06
17 changes: 12 additions & 5 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ that are special: each adds a unique piece of functionality inside your applicat
``_locale``
Used to set the locale on the request (:ref:`read more <translation-locale-url>`).

.. routing-trailing-slash-redirection:

Redirecting URLs with Trailing Slashes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -508,14 +510,19 @@ slashes (but only for ``GET`` and ``HEAD`` requests):
---------- ---------------------------------------- ------------------------------------------
Route path If the requested URL is ``/foo`` If the requested URL is ``/foo/``
---------- ---------------------------------------- ------------------------------------------
``/foo`` It matches (``200`` status response) It doesn't match (``404`` status response)
``/foo`` It matches (``200`` status response) It makes a ``301`` redirect to ``/foo``
``/foo/`` It makes a ``301`` redirect to ``/foo/`` It matches (``200`` status response)
---------- ---------------------------------------- ------------------------------------------

In summary, adding a trailing slash in the route path is the best way to ensure
that both URLs work. Read the :doc:`/routing/redirect_trailing_slash` article to
learn how to avoid the ``404`` error when the request URL contains a trailing
slash and the route path does not.
.. note::

If your application defines different routes for each path (``/foo`` and
``/foo/``) then this automatic redirection doesn't take place and the right
route is always matched.

.. versionadded:: 4.1
The automatic ``301`` redirection from ``/foo/`` to ``/foo`` was introduced
in Symfony 4.1. In previous Symfony version this results in a ``404`` response.

.. index::
single: Routing; Controllers
Expand Down
102 changes: 4 additions & 98 deletions routing/redirect_trailing_slash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,103 +4,9 @@
Redirect URLs with a Trailing Slash
===================================

The goal of this article is to demonstrate how to redirect URLs with a
trailing slash to the same URL without a trailing slash
(for example ``/en/blog/`` to ``/en/blog``).

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 308 (*HTTP Permanent Redirect*) response status code::

// src/Controller/RedirectingController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class RedirectingController extends Controller
{
public function removeTrailingSlash(Request $request)
{
$pathInfo = $request->getPathInfo();
$requestUri = $request->getRequestUri();

$url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri);

// 308 (Permanent Redirect) is similar to 301 (Moved Permanently) except
// that it does not allow changing the request method (e.g. from POST to GET)
return $this->redirect($url, 308);
}
}

After that, create a route to this controller that's matched whenever a URL
with a trailing slash is requested. Be sure to put this route last in your
system, as explained below:

.. configuration-block::

.. code-block:: php-annotations

// src/Controller/RedirectingController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class RedirectingController extends Controller
{
/**
* @Route("/{url}", name="remove_trailing_slash",
* requirements={"url" = ".*\/$"})
*/
public function removeTrailingSlash(Request $request)
{
// ...
}
}

.. code-block:: yaml

# config/routes.yaml
remove_trailing_slash:
path: /{url}
controller: App\Controller\RedirectingController::removeTrailingSlash
requirements:
url: .*/$

.. code-block:: xml

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

.. code-block:: php

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$routes = new RouteCollection();
$routes->add(
'remove_trailing_slash',
new Route(
'/{url}',
array(
'_controller' => 'App\Controller\RedirectingController::removeTrailingSlash',
),
array(
'url' => '.*/$',
)
)
);

.. caution::

Make sure to include this route in your routing configuration at the
very end of your route listing. Otherwise, you risk redirecting real
routes (including Symfony core routes) that actually *do* have a trailing
slash in their path.
In Symfony 4.1 the automatic URL redirection was improved as explained in
:ref:`routing-trailing-slash-redirection`. That's why you no longer need to
do that redirection yourself and this article has been removed because it's
no longer needed.
0