w9unhw
Last Updated: February 25, 2016
·
11.75K
· craigmarvelley

Symfony2: Redirecting a root path to that of a default locale.

While working on a Symfony2 application which uses locale prefixing of URLs to support a multilingual site, I wanted to redirect the root URL (http://mysite.com/) to that of my default locale (http://mysite.com/en/).

This can be done quite simply with routing configuration, which I learned from this cookbook article. Using the standard container parameter 'locale', which sets a default locale, we can also embed the locale value dynamically:

root:
    pattern: /
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /%locale%/
        permanent: true

Adding the above to my app/config/routing.yml file was all I needed to make /en/ the default path of my site.

2 Responses
Add your response

5244

Thanks for the simple fix!

over 1 year ago ·
22852

Thanks!!

over 1 year ago ·