8000 Documented the smart URL redirection · symfony/symfony-docs@620ba17 · GitHub
[go: up one dir, main page]

Skip to content

Commit 620ba17

Browse files
committed
Documented the smart URL redirection
1 parent f1e6802 commit 620ba17

File tree

2 files changed

+16
-103
lines changed

2 files changed

+16
-103
lines changed

routing.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ that are special: each adds a unique piece of functionality inside your applicat
493493
``_locale``
494494
Used to set the locale on the request (:ref:`read more <translation-locale-url>`).
495495

496+
.. routing-trailing-slash-redirection:
497+
496498
Redirecting URLs with Trailing Slashes
497499
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
498500

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

515-
In summary, adding a trailing slash in the route path is the best way to ensure
516-
that both URLs work. Read the :doc:`/routing/redirect_trailing_slash` article to
517-
learn how to avoid the ``404`` error when the request URL contains a trailing
518-
slash and the route path does not.
517+
.. note::
518+
519+
If your application defines different routes for each path (``/foo`` and
520+
``/foo/``) then this automatic redirection doesn't take place and the right
521+
route is always matched.
522+
523+
.. versionadded:: 4.1
524+
The automatic ``301`` redirection from ``/foo/`` to ``/foo`` was introduced
525+
in Symfony 4.1. In previous Symfony version this results in a ``404`` response.
519526

520527
.. index::
521528
single: Routing; Controllers

routing/redirect_trailing_slash.rst

Lines changed: 4 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,103 +4,9 @@
44
Redirect URLs with a Trailing Slash
55
===================================
66

7-
The goal of this article is to demonstrate how to redirect URLs with a
8-
trailing slash to the same URL without a trailing slash
9-
(for example ``/en/blog/`` to ``/en/blog``).
10-
11-
Create a controller that will match any URL with a trailing slash, remove
12-
the trailing slash (keeping query parameters if any) and redirect to the
13-
new URL with a 308 (*HTTP Permanent Redirect*) response status code::
14-
15-
// src/Controller/RedirectingController.php
16-
namespace App\Controller;
17-
18-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19-
use Symfony\Component\HttpFoundation\Request;
20-
21-
class RedirectingController extends Controller
22-
{
23-
public function removeTrailingSlash(Request $request)
24-
{
25-
$pathInfo = $request->getPathInfo();
26-
$requestUri = $request->getRequestUri();
27-
28-
$url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri);
29-
30-
8000 // 308 (Permanent Redirect) is similar to 301 (Moved Permanently) except
31-
// that it does not allow changing the request method (e.g. from POST to GET)
32-
return $this->redirect($url, 308);
33-
}
34-
}
35-
36-
After that, create a route to this controller that's matched whenever a URL
37-
with a trailing slash is requested. Be sure to put this route last in your
38-
system, as explained below:
39-
40-
.. configuration-block::
41-
42-
.. code-block:: php-annotations
43-
44-
// src/Controller/RedirectingController.php
45-
namespace App\Controller;
46-
47-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
48-
use Symfony\Component\HttpFoundation\Request;
49-
use Symfony\Component\Routing\Annotation\Route;
50-
51-
class RedirectingController extends Controller
52-
{
53-
/**
54-
* @Route("/{url}", name="remove_trailing_slash",
55-
* requirements={"url" = ".*\/$"})
56-
*/
57-
public function removeTrailingSlash(Request $request)
58-
{
59-
// ...
60-
}
61-
}
62-
63-
.. code-block:: yaml
64-
65-
# config/routes.yaml
66-
remove_trailing_slash:
67-
path: /{url}
68-
controller: App\Controller\RedirectingController::removeTrailingSlash
69-
requirements:
70-
url: .*/$
71-
72-
.. code-block:: xml
73-
74-
<?xml version="1.0" encoding="UTF-8" ?>
75-
<routes xmlns="http://symfony.com/schema/routing">
76-
<route id="remove_trailing_slash" path="/{url}" methods="GET">
77-
<default key="_controller">App\Controller\RedirectingController::removeTrailingSlash</default>
78-
<requirement key="url">.*/$</requirement>
79-
</route>
80-
</routes>
81-
82-
.. code-block:: php
83-
84-
use Symfony\Component\Routing\RouteCollection;
85-
use Symfony\Component\Routing\Route;
86-
87-
$routes = new RouteCollection();
88-
$routes->add(
89-
'remove_trailing_slash',
90-
new Route(
91-
'/{url}',
92-
array(
93-
'_controller' => 'App\Controller\RedirectingController::removeTrailingSlash',
94-
),
95-
array(
96-
'url' => '.*/$',
97-
)
98-
)
99-
);
100-
1017
.. caution::
1028

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

0 commit comments

Comments
 (0)
0