Вопрос

I have 3 subdomains , which are based on Opencart. What I need to force on each domain given language

to do so _GET['language'] should be defined strictly for each subdomain

en.handmade24.at -> language=en

ru.handmade24.at -> language=ru

www.handmade.24.at -> language=de

I was told .htaccess was a good option to force language variable...

But how can i define the rules? Any help? I am dummy in htaccess...

my Htaccess looks like this

Options +FollowSymlinks

# Prevent Directoy listing 
Options -Indexes

# Prevent Direct Access to files
<FilesMatch "\.(tpl|ini)">
 Order deny,allow
 Deny from all
</FilesMatch>

# SEO URL Settings
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

Or maybe you could offer nicer solution?

Это было полезно?

Решение

RewriteCond %{HTTP_HOST} ^(www\.)?handmade24\.at
RewriteRule ^(.*)$       $1?language=de [QSA,L]

RewriteCond %{HTTP_HOST} ^(\w+)\.handmade24\.at
RewriteRule ^(.*)$       $1?language=%1 [QSA,L]

Другие советы

Here is the .htaccess that should work for you:

Options +FollowSymlinks

# Prevent Directoy listing 
Options -Indexes

# Prevent Direct Access to files
<FilesMatch "\.(tpl|ini)">
 Order deny,allow
 Deny from all
</FilesMatch>

# SEO URL Settings
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_route_=$1 [QSA,L,NE]

RewriteCond %{HTTP_HOST} ^(www\.)?handmade24\.at$ [NC]
RewriteRule ^(.*)$ $1?language=de [QSA,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.handmade24\.at$ [NC]
RewriteRule ^(.*)$ $1?language=%1 [QSA,L]

First of all, I have taken out the OpenCart system for the project which I solved your issue, but this trick should work just fine.

Yes you can do it with .htaccess, however I do not want to recommend doing that. Messing with .htaccess can be a pain in a certain place. If you do it, at some point you will most likely end up doing something wrong and corrupt something. It isn't the only solution, and that is why I wont tell you how to do it with .htaccess.

The solution I would recommend is a neat little work-around/hack I've just made. I where looking for a way to do it with subdomains, since it is Google's preferred choice of doing the multi language versions of websites (except of TLD's of course).

I came up with the solution to overrule the default OpenCart language selection/detection.

First of all open up index.php in the root of your OpenCart install.

Go find the lines with this:

if (isset($session->data['language']) && array_key_exists($session->data['language'], $languages) && $languages[$session->data['language']]['status']){
     $code = $session->data['language'];
} elseif (isset($request->cookie['language']) && array_key_exists($request->cookie['language'], $languages) && $languages[$request->cookie['language']]['status']) {
     $code = $request->cookie['language'];
} elseif ($detect) {
     $code = $detect;
} else {
     $code = $config->get('config_language');
}

Then replace those lines with this:

$url_info = parse_url(str_replace('&amp;', '&', $config->get('config_url')));

foreach ($languages as $language)
{
    if ($language['code'] . '.' . $url_info['host'] == $request->server['HTTP_HOST'])
    {
        $overwrite_language = $language;
        break;
    }
}

if (isset($overwrite_language)) {
    $code = $overwrite_language['code'];
} elseif (isset($session->data['language']) && array_key_exists($session->data['language'], $languages) && $languages[$session->data['language']]['status']) {
    $code = $session->data['language'];
} elseif (isset($request->cookie['language']) && array_key_exists($request->cookie['language'], $languages) && $languages[$request->cookie['language']]['status']) {
    $code = $request->cookie['language'];
} elseif ($detect) {
    $code = $detect;
} else {
    $code = $config->get('config_language');
}

What this do is (note: OpenCart did load the available languages from the database a few lines earlier):

  1. We get the URL from the database ("setting table" where key = 'config_url'), then we use the PHP function parse_url, which splits it up in parts (do a print_r() on it to see what parts).

  2. We then loops through the languages that is available (those that are enabled in the admin panel).

  3. In the loop we check if the $language['code'] . '.' . $url_info['host'] == $request->server['HTTP_HOST'] (an alias of $_SERVER['HTTP_HOST']). I've put in a break so it would stop wasting resources if it already have found a matching language for the domain.

  4. I've edited the if elseif etc etc else statement that OpenCart made whith the above one. What it does is that it check if $overwrite_language is set, if it is then it makes that the language that the user " * selected * ", since it hit the if in the if elseif etc etc else statement then it wont run any of the others (which then would overwrite this little work-arround/hack).

-- Bonus tip --

If you have access to the php.ini config file, or you have permission to use ini_set(), then you can share your PHP session between subdomains

ini_set('session.cookie_domain', '.yourdomain.TLD');

(you may need to destroy an already created session). That could be an advantage, however note that it will overwrite the default shop language, but you can modify the 2 if's below the lines you just changed. The currencies should work as usual though.

-- Finishing up --

That should be it, hope you will like it and can use it, even though it is a long time you asked this question, maybe someone else could use it, just as I could a few hours ago.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top