Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 4 down vote accepted

This does work (tested):

RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteRule ^(.*)$ /en$1 [R=301,L]

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:

# If just the language is specified (ie example.com/en)
RewriteCond %{REQUEST_URI} ^/..$ [NC]
RewriteRule ^(.*)$ $1/

# If no language subfolder, default to 'en'
RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteRule ^(.*)$ /en$1 [R=301]

# If no page specified, default to home.
RewriteCond %{REQUEST_URI} !^/../.+ [NC]
RewriteRule ^/(..) /$1/home.html [R=301]

# If no ln query, tack it on
RewriteCond %{QUERY_STRING} !ln= [NC]
RewriteRule ^/(..)/(.*) /$1/$2?ln=$1 [R=301]

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 to example.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 see example.com/en/home.html the request to the page will actually be example.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.

share|improve this answer
1  
@Sam, just checked it, it works as expected. What error? Sure you got the curly brackets, parenthesizes, and braces correct? –  Chris S Dec 6 '10 at 18:56
1  
It's possible your Apache server is interpreting something slightly different; try replacing /en$1 in the rule with /en/$1. Are there any other rules in the .htaccess file? –  Chris S Dec 6 '10 at 20:00
1  
What version of Apache are you running? If you manually type in /en/somepage.php does it show up correctly? –  Chris S Dec 6 '10 at 20:47
1  
@Sam, the first and last rule are going to start a mini-war. The first rule takes anything in the form /xx/asdf and makes it /asdf?ln=xx, the last will take that and make it /xx/asdf?ln=xx, which will trigger the first one again, causing a loop. –  Chris S Dec 7 '10 at 4:20
1  
@Sam, it's probably be easiest to just edit the other rules into this question and include exactly how you want requests to be handled. –  Chris S Dec 7 '10 at 16:24

Here is the code I use. It will accept any languages and country.

# Tout pays de 2 digits et langue de 2 ou 3 digits ou juste la langue pas de pays - fonctionne bien et passe language et country
#www.country.com/index.html  devient www.country.com/index.php
#www.country.com/fr/index.html  devient www.country.com/index.php?language=fr
#www.country.com/ca/fr/index.html  devient www.country.com/index.php?country=ca&language=fr
RewriteRule ^([a-zA-Z]{2})/([a-zA-Z]{2,3})/index\.html$ index.html?country=$1&language=$2
RewriteRule ^([a-zA-Z]{2,3})/index\.html$ index.html?language=$1
RewriteRule ^index\.html$ /index.php [L]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.