8000 minor #9366 Explained how the URL trailing slash redirection works (j… · symfony/symfony-docs@eef865a · GitHub
[go: up one dir, main page]

Skip to content

Commit eef865a

Browse files
committed
minor #9366 Explained how the URL trailing slash redirection works (javiereguiluz)
This PR was merged into the 2.7 branch. Discussion ---------- Explained how the URL trailing slash redirection works This is needed so we can later in `master` explain that this has changed: https://symfony.com/blog/new-in-symfony-4-1-smarter-url-redirections Commits ------- 49ec7f0 Explained how the URL trailing slash redirection works
2 parents de2e953 + 49ec7f0 commit eef865a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

routing.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,30 @@ that are special: each adds a unique piece of functionality inside your applicat
487487
``_locale``
488488
Used to set the locale on the request (:ref:`read more <translation-locale-url>`).
489489

490+
Redirecting URLs with Trailing Slashes
491+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
492+
493+
Historically, URLs have followed the UNIX convention of adding trailing slashes
494+
for directories (e.g. ``https://example.com/foo/``) and removing them to refer
495+
to files (``https://example.com/foo``). Although serving different contents for
496+
both URLs is OK, nowadays it's common to treat both URLs as the same URL and
497+
redirect between them.
498+
499+
Symfony follows this logic to redirect between URLs with and without trailing
500+
slashes (but only for ``GET`` and ``HEAD`` requests):
501+
502+
---------- ---------------------------------------- ------------------------------------------
503+
Route path If the requested URL is ``/foo`` If the requested URL is ``/foo/``
504+
---------- ---------------------------------------- ------------------------------------------
505+
``/foo`` It matches (``200`` status response) It doesn't match (``404`` status response)
506+
``/foo/`` It makes a ``301`` redirect to ``/foo/`` It matches (``200`` status response)
507+
---------- ---------------------------------------- ------------------------------------------
508+
509+
In summary, adding a trailing slash in the route path is the best way to ensure
510+
that both URLs work. Read the :doc:`/routing/redirect_trailing_slash` article to
511+
learn how to avoid the ``404`` error when the request URL contains a trailing
512+
slash and the route path does not.
513+
490514
.. index::
491515
single: Routing; Controllers
492516
single: Controller; String naming format

0 commit comments

Comments
 (0)
0