Visualise a three step jump sporting event, with the first two steps perfect in place and the third step launching into air, never coming back and eventually a crash! That is what this question boils down to :)
STEP 1:WORKS serve /somepage?ln=xx
when user navigates to /xx/somepage
.
# if user inputs nice urls /xx/somepage to serve page /someapge?ln=xx
RewriteRule ^([a-z][a-z])/(.*) /$2?ln=$1 [L]
STEP 2:WORKS go to the homepage when only language is given /xx/
# if language xx given but no page given? then redirect to its home /xx/home
RewriteRule ^([a-z][a-z])/?$ /$1/home [R=301,L]
STEP 3:CRASH
# if user inputs /somepage then redirect to default english: /en/somepage
# if user inputs /somepage?ln=xx redirect to nice url /xx/somepage
RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteRule ^(.*)$ /en/$1 [R=301,L]
The third rule is where we are stuck... PS: The redirection should work both for extensionless files somepage?ln=xx as well as files with extension somepage.abc?ln=xx where the extension can be any 2 or 3 char-word, or if easier, checked manually with the following extensions i use [.php .uu .u3c .vls]
Thanks a Thousand!
This does work (tested):
Apache Mod_Rewrite documentation, Rewrite Guide, and Advanced Rewrite Guide.
Edit:
Tested the above (it's the same as it was before); works on my Apache 2.2 server.
Edit 2:
Should be no problem, just need something like this:
Note: Be careful your 404 page works correctly. If it doesn't invalid links will end up in an infinite loop (ie, the 404 page wont work).
Note 2: If the user types in
example.com/en
, they will be redirected toexample.com/en/en?ln=en
, so be sure the tailing slash is in the URL, or else.Note 3: If you want, you can drop the
[R=301]
from the last rule, then users will seeexample.com/en/home.html
the request to the page will actually beexample.com/en/home.html?ln=en
. This will not work if your site uses GET requests though.Edit 3:
Added another cond/rule pair to catch if someone types in just the language, without a trailing slash.
Here is the code I use. It will accept any languages and country.