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.
Written by Craig Marvelley
Related protips
2 Responses
Thanks for the simple fix!
over 1 year ago
·
Thanks!!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Routing
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#