Upcoming Official Symfony Conferences
WARNING: You are browsing the documentation for Symfony 2.1 which is not maintained anymore. Consider upgrading your projects to Symfony 4.1.

How to configure a redirect to another route without a custom controller

2.1 version
Unmaintained

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 the path parameter containing the path of the resource you want to redirect to.
  • redirect (not shown here) redirects to another route. You must provide the route 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.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.