4
4
Redirect URLs with a Trailing Slash
5
5
===================================
6
6
7
- The goal of this cookbook is to demonstrate how to redirect URLs with
7
+ The goal of this cookbook is to demonstrate how to redirect URLs with a
8
8
trailing slash to the same URL without a trailing slash
9
9
(for example ``/en/blog/ `` to ``/en/blog ``).
10
10
11
- You have to create a controller that will match any URL with a trailing
12
- slash, remove the trailing slash (keeping query parameters if any) and
13
- redirect to the new URL with a 301 response status code::
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 301 response status code::
14
14
15
15
// src/Acme/DemoBundle/Controller/RedirectingController.php
16
16
namespace Acme\DemoBundle\Controller;
@@ -31,8 +31,9 @@ redirect to the new URL with a 301 response status code::
31
31
}
32
32
}
33
33
34
- And after that, register this controller to be executed whenever a URL
35
- with a trailing slash is requested:
34
+ After that, create a route to this controller that's matched whenever a URL
35
+ with a trailing slash is requested. Be sure to put this route last in your
36
+ system, as explained below:
36
37
37
38
.. configuration-block ::
38
39
@@ -79,12 +80,13 @@ with a trailing slash is requested:
79
80
80
81
.. note ::
81
82
82
- Redirecting a POST request does not work well in old browsers.
83
- A 302 on a POST request will send a GET request after the
84
- redirection for legacy reasons .
83
+ Redirecting a POST request does not work well in old browsers. A 302
84
+ on a POST request would send a GET request after the redirection for legacy
85
+ reasons. For that reason, the route here only matches GET requests .
85
86
86
87
.. caution ::
87
88
88
- Make sure to include this route in your routing configuration at
89
- the very end of your route listing. Otherwise, you risk to redirect
90
- Symfony2 core routes that natively do have a trailing slash.
89
+ Make sure to include this route in your routing configuration at the
90
+ very end of your route listing. Otherwise, you risk redirecting real
91
+ routes (including Symfony2 core routes) that actually *do * have a trailing
92
+ slash in their path.
0 commit comments