How to configure a redirect to another route without a custom controller
Edit this pageWarning: You are browsing the documentation for Symfony 2.1, which is no longer maintained.
Read the updated version of this page for Symfony 6.0 (the current stable version).
How to configure a redirect to another route without a custom controller
This guide explains how to configure a redirect from one route to another without using a custom controller.
Assume that there is no useful default controller for the /
path of
your application and you want to redirect these requests to /app
.
Your configuration will look like this:
1 2 3 4 5 6 7 8 9 10 11
AppBundle:
resource: "@App/Controller/"
type: annotation
prefix: /app
root:
pattern: /
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /app
permanent: true
In this example, you configure a route for the /
path and let RedirectController
handle it. This controller comes standard with Symfony and offers two actions
for redirecting request:
urlRedirect
redirects to another path. You must provide thepath
parameter containing the path of the resource you want to redirect to.redirect
(not shown here) redirects to another route. You must provide theroute
parameter with the name of the route you want to redirect to.
The permanent
switch tells both methods to issue a 301 HTTP status code
instead of the default 302
status code.