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 mysite.com/en/ is the English version of the homepage, mysite.com/fr/ is the French and mysite.com/ru/ is the Russian. Even though the /en/ folders etc. don't actually exist - they're just aliases for mysite.com/index.php?lang=en 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: myenglishsite.com, myfrenchsite.com and myrussiansite.com. They are all parked at the same IP address, so that myenglishsite.com is exactly equivalent to myrussiansite.com.

Here's what I want to happen:
- I want myenglishsite.com to automatically redirect to myenglishsite.com/en/
- I want myfrenchsite.com to automatically redirect to myfrenchsite.com/fr/
- I want myrussiansite.com to automatically redirect to myrussiansite.com/ru/

but - and here's the pain in the ass - I need myenglishsite.com/admin/ to stay as it is, not to have /en/ inserted rudely into its middle, because myenglishsite.com/en/admin 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 myrussiansite.com/ru/page/sub-page/ into the browser will result in myrussiansite.com/ru/ru/page/sub-page/ 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 myenglishsite.com/ to myenglishsite.com/en/ but if the address has anything beyond the root domain, i.e. we're looking for myenglishsite.com/* then take no action, regardless of what * is.

This next bit of code, for instance:

RewriteCond %{HTTP_HOST} myenglishsite.com
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 myenglishsite.com/x/ where x is NOT /en/ gets one. So the stylesheet can't be found, because myenglishsite.com/en/style/style.css is not the correct address.

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

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 myrussiansite.com/fr/ should be in French (going back to what I said about 'take no action in the case of domain.com/* 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:
<?php

$lang = 'en/'; # default
if ($_SERVER['HTTP_HOST'] == 'myfrenchsite.com') {
  $lang = 'fr/';
} elseif ($_SERVER['HTTP_HOST'] == 'myrussiansite.com') {
  $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


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


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


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.