Skip to content

[Router] Optionnaly disable the trailing slash redirection behavior #33342

Closed

Description

@xavierleune

Hi,

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-grekas here - 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

Activity

  1. xavierleune commented on Aug 27, 2019

    @xavierleune
    ContributorAuthor

    Closing in favor of #32996

  2. xavierleune commented on Sep 2, 2019

    @xavierleune
    ContributorAuthor

    @Jean-ita do you have an opinion on this ?

  3. nicolas-grekas commented on Sep 8, 2019

    @nicolas-grekas
    Member

    Use UrlMatcher instead of RedirectableUrlMatcher:

    did you give this a try? Maybe that's something that should be documented better and done?

  4. stof commented on Sep 9, 2019

    @stof
    Member

    Well, that also disable some other features

  5. xavierleune commented on Sep 13, 2019

    @xavierleune
    ContributorAuthor

    @nicolas-grekas no I didn't, as @stof said, that also disable other features & I don't think that's a good alternative.

  6. boldtrn commented on Dec 8, 2019

    @boldtrn

    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 👍.

  7. perk11 commented on Sep 30, 2020

    @perk11

    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.

  8. s3bul commented on Oct 21, 2020

    @s3bul

    In my opinion url with / must be distinguished.
    I have a situation in my router config

    event_controllers:
      resource: ../../src/Module/Event/Controller/Api
      type: annotation
      name_prefix: event_
      prefix: /api/v1/event

    and my controller looks like this

    namespace App\Module\Event\Controller\Api;
    
    (...)
    
    /**
     * Class EventController
     *
     * @package App\Module\Event\Controller\Api
     *
     * @Rest\Route("", name="event_")
     */
    class EventController extends ApiController
    {
        /**
         * @return View
         *
         * @Rest\Get("", name="index")
         */
        public function index(): View

    and in my swagger (i using nelmio/api-doc-bundle:^3.4) i have
    Zrzut ekranu z 2020-10-21 10-35-58
    it doesn't make sense...

  9. ivanbogomoloff commented on Nov 16, 2020

    @ivanbogomoloff

    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 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"="\/"})
         */
        public function foundBySlug()
        {
            return new Response("ok");
        }
    
        /**
         *  @Route(path="/{slug}", name="hack_trailing_slash_404_redirect")
         */
        public function notFoundSlug()
        {
            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/

  10. nicolas-grekas commented on Nov 16, 2020

    @nicolas-grekas
    Member

    @ivanbogomoloff you can match a route with a forced trailing / with this definition: /{slug}{_</(?!/)>}

  11. ivanbogomoloff commented on Nov 16, 2020

    @ivanbogomoloff

    @ivanbogomoloff you can match a route with a forced trailing / with this definition: /{slug}{_</(?!/)>}

    Not working.
    I try this early and now try and confirm that it not work

    /**
         * @Route(path="/{slug}{_</(?!/)>}", name="slug_slash")
         */
        public function foundBySlug()
        {
            return new Response("ok");
        }

    When i try /anything - this will redirect me to /anything/ and give me 200 ok.

    I found another solution, but it scary too

    /**
         * @Route(path="/{slug}{slash}", name="slug_slash", requirements={"slash" = ".+"})
         */
        public function foundBySlug(Request $request)
        {
            $url = $request->getPathInfo();
            if(!preg_match('/\/$/i', $url)) {
                // /anything will give 404
                throw $this->createNotFoundException();
            }
            // /anything/ give ok
            return new Response("ok");
        }
  12. nicolas-grekas commented on Nov 16, 2020

    @nicolas-grekas
    Member

    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.

  13. 21 remaining items

  14. carsonbot commented on Mar 16, 2023

    @carsonbot

    Hello? This issue is about to be closed if nobody replies.

  15. jontjs commented on Mar 16, 2023

    @jontjs
    Contributor

    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.

  16. ssidorchik-zz commented on Jul 27, 2023

    @ssidorchik-zz

    is there still no option to turn off this behavior?

  17. carsonbot commented on Jan 28, 2024

    @carsonbot

    Thank you for this issue.
    There has not been a lot of activity here for a while. Has this been resolved?

  18. perk11 commented on Jan 28, 2024

    @perk11

    @carsonbot As far as I'm aware this was not implemented yet.

  19. carsonbot commented on Jul 29, 2024

    @carsonbot

    Thank you for this issue.
    There has not been a lot of activity here for a while. Has this been resolved?

  20. carsonbot commented on Aug 12, 2024

    @carsonbot

    Could I get a reply or should I close this?

  21. Jean-Gian commented on Aug 19, 2024

    @Jean-Gian

    It can be close in my opinion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions