Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]


# 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 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
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.

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
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, they will be redirected to, 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 the request to the page will actually be 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
@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
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
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
@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
@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  devient  devient  devient
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


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.