I'd like to force my site's URL to always have a language suffix.
So, if they type www.mysite.com
it should take them to www.mysite.com/en
.
I have a default language, so that should be used if it's their first time to the site. If it's not, I have a Cookie being set that I can use...but - I don't know where to use it.
I thought about checking to see if there was a "language" parameter in the URL, then if not, redirecting, but - that seems overkill - is there a better way? Can I do this in routes? or bootstrap?
What I did:
I ended up checking in the AppController's beforeFilter() whether or not
$this->request->params['langauge']
was set and if not, building the URL accordingly:Note:
The part I couldn't figure out (until getting help in IRC) was to build the URL using
$this->request->here
, which is just the URL as a string. Prior to that I tried building out the array using the params array, but had no luck.My routes (in case they help anyone)
(Keep in mind, I'm a routes noob, so - although they seem to be working for me, I do NOT guarantee they're done well!)
The most efficient way would be through your web server. You can easily check if the request is for
/
(the home page) and redirect to/en
.Check the docs for what ever web server you are using, they all have something like
mod_rewrite
or similar.Edit
You could set up a route like
/set_default_language
to redirect to in case of/
, this controller can access the db and do what ever it needs.Alternatively you can make it redirect to
/your/usual/language_switch
with no language specified and allow the code to use the default.If you don't want to redirect the requestAction calls from within your controllers or views you should add the following condition to your IF statement