Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Routing
  4. How to Configure a Redirect without a custom Controller
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Redirecting Using a Path
  • Redirecting Using a Route

How to Configure a Redirect without a custom Controller

Edit this page

Warning: You are browsing the documentation for Symfony 3.4, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

How to Configure a Redirect without a custom Controller

Sometimes, a URL needs to redirect to another URL. You can do that by creating a new controller action whose only task is to redirect, but using the RedirectController of the FrameworkBundle is even easier.

You can redirect to a specific path (e.g. /about) or to a specific route using its name (e.g. homepage).

Redirecting Using a Path

Assume there is no default controller for the / path of your application and you want to redirect these requests to /app. You will need to use the urlRedirectAction() action to redirect to this new URL:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# app/config/routing.yml

# load some routes - one should ultimately have the path "/app"
AppBundle:
    resource: '@AppBundle/Controller/'
    type:     annotation
    prefix:   /app

# redirecting the root
root:
    path: /
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /app
        permanent: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- app/config/routing.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        https://symfony.com/schema/routing/routing-1.0.xsd">

    <!-- load some routes - one should ultimately have the path "/app" -->
    <import resource="@AppBundle/Controller/"
        type="annotation"
        prefix="/app"
    />

    <!-- redirecting the root -->
    <route id="root" path="/">
        <default key="_controller">FrameworkBundle:Redirect:urlRedirect</default>
        <default key="path">/app</default>
        <default key="permanent">true</default>
    </route>
</routes>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// app/config/routing.php
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

$routes = new RouteCollection();

// load some routes - one should ultimately have the path "/app"
$appRoutes = $loader->import("@AppBundle/Controller/", "annotation");
$appRoutes->setPrefix('/app');

$routes->addCollection($appRoutes);

// redirecting the root
$routes->add('root', new Route('/', [
    '_controller' => 'FrameworkBundle:Redirect:urlRedirect',
    'path'        => '/app',
    'permanent'   => true,
]));

return $routes;

In this example, you configured a route for the / path and let the RedirectController redirect it to /app. The permanent switch tells the action to issue a 301 HTTP status code instead of the default 302 HTTP status code.

Redirecting Using a Route

Assume you are migrating your website from WordPress to Symfony, you want to redirect /wp-admin to the route sonata_admin_dashboard. You don't know the path, only the route name. This can be achieved using the redirectAction() action:

1
2
3
4
5
6
7
8
9
10
11
# app/config/routing.yml

# ...

# redirecting the admin home
root:
    path: /wp-admin
    defaults:
        _controller: FrameworkBundle:Redirect:redirect
        route: sonata_admin_dashboard
        permanent: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- app/config/routing.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        https://symfony.com/schema/routing/routing-1.0.xsd">

    <!-- ... -->

    <!-- redirecting the admin home -->
    <route id="root" path="/wp-admin">
        <default key="_controller">FrameworkBundle:Redirect:redirect</default>
        <default key="route">sonata_admin_dashboard</default>
        <default key="permanent">true</default>
    </route>
</routes>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// app/config/routing.php
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

$routes = new RouteCollection();
// ...

// redirecting the root
$routes->add('root', new Route('/wp-admin', [
    '_controller' => 'FrameworkBundle:Redirect:redirect',
    'route'       => 'sonata_admin_dashboard',
    'permanent'   => true,
]));

return $routes;

Caution

Because you are redirecting to a route instead of a path, the required option is called route in the redirect() action, instead of path in the urlRedirect() action.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Bizley, a Symfony contributor

    Thanks Bizley for being a Symfony contributor

    2 commits • 34 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch