通过 htaccess 强制语言 GET 变量还是...?

发布于 2024-11-07 10:33:09 字数 675 浏览 0 评论 0原文

我有 3 个子域,它们基于 Opencart。 我需要对给定语言的每个域强制执行

_GET['language'] 应该为每个子域 en.handmade24.at 严格定义

->语言=en

ru.handmade24.at ->语言=ru

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

我被告知 .htaccess 是强制语言变量的一个不错的选择...

但是我如何定义规则?有什么帮助吗?我在 htaccess 中是虚拟的...

我的 Htaccess 看起来像这样

选项 +FollowSymlinks

# 防止目录列表 
选项-索引

# 防止直接访问文件

 订单拒绝、允许
 所有人都否认


# SEO 网址设置
重写引擎开启
重写库 /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

或者也许你可以提供更好的解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子的影子 2024-11-14 10:33:09
RewriteCond %{HTTP_HOST} ^(www\.)?handmade24\.at
RewriteRule ^(.*)$       $1?language=de [QSA,L]

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

RewriteCond %{HTTP_HOST} ^(\w+)\.handmade24\.at
RewriteRule ^(.*)$       $1?language=%1 [QSA,L]
清眉祭 2024-11-14 10:33:09

这是应该适合您的 .htaccess:

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]

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]
反话 2024-11-14 10:33:09

首先,我已经为解决你的问题的项目取出了 OpenCart 系统,但是这个技巧应该可以正常工作。

是的,您可以使用 .htaccess 来做到这一点,但我不建议这样做。在某些地方,弄乱 .htaccess 可能会很痛苦。如果你这样做,在某些时候你很可能最终会做错事并腐败一些东西。这不是唯一的解决方案,这就是为什么我不会告诉您如何使用 .htaccess 来做到这一点。

我推荐的解决方案是我刚刚制作的一个巧妙的小解决方法/技巧。我正在寻找一种使用子域来实现这一点的方法,因为它是 Google 制作网站多语言版本的首选(当然 TLD 除外)。

我想出了解决方案来推翻默认的 OpenCart 语言选择/检测。

首先,在 OpenCart 安装的根目录中打开 index.php。

找到以下行:

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');
}

然后用以下内容替换这些行:

$url_info = parse_url(str_replace('&', '&', $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');
}

这是做什么的(注意:OpenCart 之前几行从数据库中加载了可用语言):

  1. 我们从数据库中获取 URL(“设置表”) “其中 key = 'config_url'),然后我们使用 PHP 函数 parse_url,它将其分成几部分(对其执行 print_r() 以查看哪些部分)。

  2. 然后,我们循环访问可用的语言(在管理面板中启用的语言)。

  3. 在循环中,我们检查是否 $language['code'] 。 '.' 。 $url_info['host'] == $request->server['HTTP_HOST']$_SERVER['HTTP_HOST'] 的别名) 。我已经添加了一个 break,这样如果它已经找到了该域的匹配语言,它就会停止浪费资源。

  4. 我已经编辑了 OpenCart 使用上述语句制作的 if elseif 等 else 语句。它的作用是检查 $overwrite_language 是否已设置,如果设置了,则它使用户“ * 选择 * ”的语言,因为它命中了 if elseif 等中的 if 等 else语句,那么它就不会运行任何其他语句(然后会覆盖这个小工作环境/黑客)。

-- 额外提示 --

如果您有权访问 php.ini 配置文件,或者您有权使用 ini_set(),那么您可以在子域之间共享 PHP 会话

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

(您可能需要销毁已创建的会话)。这可能是一个优势,但请注意,它将覆盖默认的商店语言,但您可以修改刚刚更改的行下方的 2 个 if。不过,货币应该照常运作。

-- 结束 --

应该就是这样了,希望你会喜欢它并且能够使用它,尽管你问这个问题已经很久了,也许其他人也可以使用它,就像我几个小时前一样。

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('&', '&', $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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文