-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from all commits
Commits
File filter
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
commit e68d8d2aca68473b371fd495264b5451f8e4352e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ Routing | |
method_parameters | ||
service_container_parameters | ||
custom_route_loader | ||
redirect_trailing_slash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
.. 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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 !
There was a problem hiding this comment.
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 ?