htaccess multi-domain redirect issues
September 19, 2010 6:16 PM   Subscribe

I have .htaccess rewrite/redirect problems. What makes this more frustrating is that it is such a simple situation.

I am doing a favour for a friend, the kind of favour that you soon wish you'd never got involved with and left for a professional to sort out. But I'm a nice person, so I get to spend my weekend gnawing my own arms off in anger at how out of my depth I am.

I have a multilingual CMS site (English, French, Russian) and I am attempting to have a separate domain for each language, even though all the content is on the same server.

A CMS plugin handles the multilingual stuff - each page has English content, French content and Russian content, but when the page is compiled for display (it's a php/MySQL driven database) only the active language is displayed. So is the English version of the homepage, is the French and is the Russian. Even though the /en/ folders etc. don't actually exist - they're just aliases for or some such thing. I'd assumed that this was an htaccess thing, so expected to see a corresponding Rewrite rule in the existing htaccess, but there isn't one, so I don't know exactly how the alias is happening. That might not be important - what is important is that foo/en/bar is the English version of foo/fr/bar etc, and that the /lang/ bit is not a real directory.

I have three domains:, and They are all parked at the same IP address, so that is exactly equivalent to

Here's what I want to happen:
- I want to automatically redirect to
- I want to automatically redirect to
- I want to automatically redirect to

but - and here's the pain in the ass - I need to stay as it is, not to have /en/ inserted rudely into its middle, because is not a valid address and I'll never be able to log in to the site's admin area. And obviously address that already have language information need to stay untouched, otherwise typing into the browser will result in being output, which will give an error.

Every .htaccess rule I've tried so far has either resulted in a 500 Internal Server Error, or /lang/ being inserted into every address, or some other weird thing where the CSS is no longer loaded and images can't be found, or nothing at all.

What I want to say, in plain English is this:
Redirect the root domain to but if the address has anything beyond the root domain, i.e. we're looking for* then take no action, regardless of what * is.

This next bit of code, for instance:

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

results in /en/ successfully being appended to the domain, but it's too successful, because every single instance of where x is NOT /en/ gets one. So the stylesheet can't be found, because is not the correct address.

what I want is the ^(.*)$ bit [as I understand it, this means 'any number of characters'] to say$ (i.e. with or without trailing slash, and nothing else after it). But if I put that in the htaccess, nothing whatsoever appears to happen and does not forward to

Failing this, how else can I get the three root domains to point to the correct languages? I should add that the language suffix is more important than the domain, so should be in French (going back to what I said about 'take no action in the case of* where * is something')

Can anyone help? Please?
posted by The Discredited Ape to Computers & Internet (4 answers total) 3 users marked this as a favorite
mod_rewrite is great, but in this case I think it is a little overkill. You just want to do a redirection for the index page, so create an index.php with this:

$lang = 'en/'; # default
if ($_SERVER['HTTP_HOST'] == '') {
  $lang = 'fr/';
} elseif ($_SERVER['HTTP_HOST'] == '') {
  $lang = 'ru/';
} # etc...

header( sprintf( "Location: http://%s/%s", $_SERVER['HTTP_HOST'], $lang ), true, 302 );


posted by sbutler at 7:14 PM on September 19, 2010

Best answer: If you just want to match the root only, then you want RewriteRule ^$ en/ [R]. You don't even need the RewriteCond checking for /en because such a RewriteCond would never match that anyway.
posted by Rhomboid at 7:25 PM on September 19, 2010

Best answer: Or, if you're really hell bent on using mod_rewrite, a RewriteRule like this should work (although sometimes I get confused about when a subreq happens to add a trailing /)
# If you're doing this in an .htaccess file you need RewriteBase
RewriteBase /
# Some RewriteCond statements...
RewriteRule ^/?$ /en/ [R,L]
Note that RewriteRule doesn't match against the whole URL, just the path part. And inside an .htaccess, I believe the beginning '/' is always stripped.
posted by sbutler at 7:27 PM on September 19, 2010

Response by poster: I love you both so much I could weep. Huge, heaving sobs of joy.
posted by The Discredited Ape at 7:32 PM on September 19, 2010

« Older Sandwiches of the future.   |   How do you say "homeless" in Swedish? Newer »
This thread is closed to new comments.