w9unhw
Last Updated: February 25, 2016
·
10.58K
· craigmarvelley
44c6fcfb46945625463e1847e1dca048

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.

Say Thanks
Respond

2 Responses
Add your response

5244
48f3b9e42cd9e2fe39c7ec78e5119349

Thanks for the simple fix!

over 1 year ago ·
22852
Cac2c3b7aa094c9ce8ce40e255c04717 normal

Thanks!!

over 1 year ago ·