Hi there- I'm developing a multilingual site, and I'm using language identifiers in urls in conjunction with a front controller such that visiting
http://www.domain.tld/en/content
or
http://www.domain.tld/de/conent
will pull up content localised in those languages. If the site is visited without specifying a language identifier, the users browsers language settings are detected via Accept-Language as a best guess, and then redirected. If Accept-Language cannot be detected, then it defaults to english. So, for example, if you visit
http://www.domain.tld/content
and your browsers language settings are in german and Accept-Language detects de, you'll be redirected to
http://www.domain.tld/de/content
If your browsers language settings are not supported, you'll be redirected to the default supported language, english
http://www.domain.tld/en/content
This works fine... as long as don't manually put in a url with an unsupported language identifier at the beginning of the url. To explain, I have mod_rewrite set up such that urls that dont point to existing files or folders invoke the front controller. That being the case, if I purposefully put in an unsupported language code in the browser, such as 'it',
http://www.domain.tld/it/content
The front controller is invoked, but I want to avoid this, instead prefering to have Apache filter requests such that only supported languages are provided to the front controller.
Essentially what I want is to be able to define a rule such that if a url does not start with a supported language identifier, that users will abe redirected to a url that replaces the unsupported language code with the default one (en) without invoking the front controller.
This is my config file:
#
# Access rights and behaviours for DocumentRoot
#
<Directory /WWW>
#
Options +FollowSymLinks +Indexes -MultiViews
# Allow requests for all
Order allow,deny
Allow from all
# Don't use access rules defined in child directories
AllowOverride None
# Define the router as the default index page
DirectoryIndex /router.php
# Turn on url rewriting
RewriteEngine on
# If we encounter a request to document root and can detect a language preference, grab it...
RewriteCond %{HTTP:Accept-Language} ^(en|de|fr)[-,;]? [NC,OR]
# ... or use the default language identifier (english) ...
RewriteCond en ^(en)$
# ... and redirect the request to a path prefixed with the language identifier
RewriteRule ^$ /%1/ [R=301,L]
# Mystery rule here!
# If the requested file does not exist...
RewriteCond %{REQUEST_FILENAME} !-f
# ... or if the requested directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# ...forward the request to the router
RewriteRule ^(.*)$ router.php/$1 [L]
</Directory>
Where I've put the "Mystery rule here!" comment is where I need to define a rule such that if the request uri does not begin with a supported language code, that en will be used in place.
I've tried doing this a number of ways, but keep falling into redirect loops. To example what I'm trying to do
RewriteCond %{REQUEST_URI} !^/(en|de|fr).$
RewriteRule ^$ /en/$1 [R=301,L]
What this is supposed to represent is:
If the request uri does not begin with en, de or fr, grab the remainder of the uri, prefix it with en, then redirect for the front controller to intercept.
Can anyone advise me? Thanks!