|
4 | 4 | Redirect URLs with a Trailing Slash
|
5 | 5 | ===================================
|
6 | 6 |
|
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 |
| -
|
101 | 7 | .. caution::
|
102 | 8 |
|
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