You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Router] Optionnaly disable the trailing slash redirection behavior #33342
Since SF 3.4 4.1, a new behavior regarding trailing slashes has been implemented. An other version of this behavior has been around forever. This behavior, largely documented, can be a little bit confusing and lead to some strange redirect in large applications.
As there is no real standard about this kind of redirect, I think this could be a better option to add a configuration key, to optionally disable all the automatic redirect to trailing slash or no stuff.
Following the comments on #32996 and #33362 this can be a valid use case to avoid a SEO risk.
Multiple options can be imagined to disable this behavior, with some pros and cons:
Global behavior:
General considerations:
Pros
Cons
Easy to use, easy to understand
If some 3rd party bundle relies on this feature, it can break things (suggested by @nicolas-grekashere - point 2 but not sure about a real use case here)
Use UrlMatcher instead of RedirectableUrlMatcher:
Pros
Cons
No code, ready to use
Disable also https/http redirections and 405
Add a configuration key:
Pros
Cons
Easy to understand and to document
Very predictable
Local behavior:
General considerations:
Pros
Cons
Greater control about this behavior
Easy to forget
Unpredictable outcome if 2 concurrent routes have different configurations
Use and document the syntax described here (regex with negative lookahead to forbid final slash if unwanted):
Pros
Cons
No code in sf
Hard to read and to debug
Ready to use for dev
Option on route declaration
Pros
Cons
Easy to understand and to use
What do you think ? I'll update the list if I think to more pros / cons
Thanks,
--
Edited on 2019-08-30 with more precise description
Reacted by Konstantin Pereiaslov, Nelson Martell and Cedric Sana
Just to add another point in favor of disabling the trailing slash redirection behavior:
The automatic redirect can break urls if they use query parameter arrays - for example: example.com/path/?foo=bar1&foo=bar2 will be redirected to example.com/path/?foo=bar1. In earlier version of Symfony I could create my own redirection code that handles these issues, now I can't. So an easy option to keep BC here would be great 👍.
The documentation explains this as "nowadays it’s common to treat both URLs as the same URL and redirect between them".
Yes, it's common, but not universal. The URLs are exposed to the end user. This is something where the framework shouldn't get in the way. If I want to have an application that doesn't follow this, Symfony is not providing a proper way to do this without losing some other features.
I think adding a setting that would be checked in addition to whether RedirectableUrlMatcherInterface is implemented here https://github.com/symfony/routing/blob/7485aa5c8999dff509d1b433339a6b131dcdf845/Matcher/UrlMatcher.php#L135 would be a good way to resolve this for people that get struck by this behavior and have no other choice but use an UrlMatcher that doesn't implement RedirectableUrlMatcherInterface and lose some features.
This feature will be useful.
In my case i need url /anything/ - this url i want handle by controller, and sent 200 or 30x or 404 etc, and i have /anything - that must be 404
I solve this by 2 routes with ordering defintion.
//ordering of methods is important, if notFoundSlug will be first then it not working /** * @Route(path="/{slug}{slash}", name="hack_trailing_slash_200_ok", requirements={"slash"="\/"}) */publicfunctionfoundBySlug()
{
returnnewResponse("ok");
}
/** * @Route(path="/{slug}", name="hack_trailing_slash_404_redirect") */publicfunctionnotFoundSlug()
{
throw$this->createNotFoundException();
}
But if i remove notFoundSlug method, url /anything/ and url /anything - always be 200 OK - this is incorrect behavior becase /anything != /anything/
When i try /anything - this will redirect me to /anything/ and give me 200 ok.
Yes, that's because you'd also need to define (after the one with a slash) /anything and map it to a 404. Otherwise, the redirection logic does it magic yes.
I found another solution, but it scary too
It's how catch-all controllers are supposed to work yes.
I still agree with what @perk11 said earlier in this issue:
The documentation explains this as "nowadays it’s common to treat both URLs as the same URL and redirect between them".
Yes, it's common, but not universal. The URLs are exposed to the end user. This is something where the framework shouldn't get in the way. If I want to have an application that doesn't follow this, Symfony is not providing a proper way to do this without losing some other features.
An option to disable this particular "magic" behaviour would be welcome.
Activity
xavierleune commentedon Aug 27, 2019
Closing in favor of #32996
xavierleune commentedon Sep 2, 2019
@Jean-ita do you have an opinion on this ?
nicolas-grekas commentedon Sep 8, 2019
did you give this a try? Maybe that's something that should be documented better and done?
stof commentedon Sep 9, 2019
Well, that also disable some other features
xavierleune commentedon Sep 13, 2019
@nicolas-grekas no I didn't, as @stof said, that also disable other features & I don't think that's a good alternative.
boldtrn commentedon Dec 8, 2019
Just to add another point in favor of disabling the trailing slash redirection behavior:
The automatic redirect can break urls if they use query parameter arrays - for example:
example.com/path/?foo=bar1&foo=bar2will be redirected toexample.com/path/?foo=bar1. In earlier version of Symfony I could create my own redirection code that handles these issues, now I can't. So an easy option to keep BC here would be great 👍.perk11 commentedon Sep 30, 2020
The documentation explains this as "nowadays it’s common to treat both URLs as the same URL and redirect between them".
Yes, it's common, but not universal. The URLs are exposed to the end user. This is something where the framework shouldn't get in the way. If I want to have an application that doesn't follow this, Symfony is not providing a proper way to do this without losing some other features.
I think adding a setting that would be checked in addition to whether RedirectableUrlMatcherInterface is implemented here https://github.com/symfony/routing/blob/7485aa5c8999dff509d1b433339a6b131dcdf845/Matcher/UrlMatcher.php#L135 would be a good way to resolve this for people that get struck by this behavior and have no other choice but use an UrlMatcher that doesn't implement RedirectableUrlMatcherInterface and lose some features.
s3bul commentedon Oct 21, 2020
In my opinion url with
/must be distinguished.I have a situation in my router config
and my controller looks like this
and in my swagger (i using nelmio/api-doc-bundle:^3.4) i have

it doesn't make sense...
ivanbogomoloff commentedon Nov 16, 2020
Hello all.
This feature will be useful.
In my case i need url
/anything/- this url i want handle by controller, and sent 200 or 30x or 404 etc, and i have/anything- that must be 404I solve this by 2 routes with ordering defintion.
But if i remove
notFoundSlugmethod, url/anything/and url/anything- always be 200 OK - this is incorrect behavior becase/anything!=/anything/nicolas-grekas commentedon Nov 16, 2020
@ivanbogomoloff you can match a route with a forced trailing
/with this definition:/{slug}{_</(?!/)>}ivanbogomoloff commentedon Nov 16, 2020
Not working.
I try this early and now try and confirm that it not work
When i try
/anything- this will redirect me to/anything/and give me 200 ok.I found another solution, but it scary too
nicolas-grekas commentedon Nov 16, 2020
Yes, that's because you'd also need to define (after the one with a slash)
/anythingand map it to a 404. Otherwise, the redirection logic does it magic yes.It's how catch-all controllers are supposed to work yes.
21 remaining items
carsonbot commentedon Mar 16, 2023
Hello? This issue is about to be closed if nobody replies.
jontjs commentedon Mar 16, 2023
I still agree with what @perk11 said earlier in this issue:
An option to disable this particular "magic" behaviour would be welcome.
ssidorchik-zz commentedon Jul 27, 2023
is there still no option to turn off this behavior?
carsonbot commentedon Jan 28, 2024
Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?
perk11 commentedon Jan 28, 2024
@carsonbot As far as I'm aware this was not implemented yet.
carsonbot commentedon Jul 29, 2024
Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?
carsonbot commentedon Aug 12, 2024
Could I get a reply or should I close this?
Jean-Gian commentedon Aug 19, 2024
It can be close in my opinion