New in Symfony 4.1: 307 and 308 redirections

Contributed by
Paweł Smoliński
in #26213.

In web applications is common to use the 301 HTTP status code to make permanent redirections and the 302 code to make temporary redirections. However, a problem with these redirection types is that a POST request is transformed into a GET request when doing the redirection for legacy reasons (redirecting a POST request didn't work well in old browsers).

In order to solve these issues, the HTTP standard introduced two new status codes:

  • 307 (Temporary Redirect): defined in RFC 7231 and similar to 302 (Found), except that it does not allow changing the request method from POST to GET.
  • 308 (Permanent Redirect): defined in RFC 7538 and similar to 301 (Moved Permanently), except that it does not allow changing the request method from POST to GET.

In Symfony 4.1 we added support for these new redirection types by adding a new keepRequestMethod argument to the redirectAction() and urlRedirectAction() methods of the RedirectController. You can also use it as the keepRequestMethod option in the route definitions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
route_301:
    # ...
    defaults:
        # ...
        permanent: true

route_302:
    # ...
    defaults:
        # ...
        permanent: false

route_307:
    # ...
    defaults:
        # ...
        permanent: false
        keepRequestMethod: true

route_308:
    # ...
    defaults:
        # ...
        permanent: true
        keepRequestMethod: true

Comments

Did I miss something or should it be: ...is transformed into a GET s/response/request/ when...?
@Franklin you are right! I've fixed it. Thanks for the heads up!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.