Magento 多语言 - 404 中语言结果的双重更改(或如何更改商店而不是视图中的语言)

发布于 2025-01-01 13:13:25 字数 2458 浏览 3 评论 0原文

我在安装 magento 时遇到问题。我用的是 Magento 版本。 1.5.0.1,社区版开发本网站http://cissmarket.com/

当我将语言从欧盟版本更改为法语,然后更改为德语时,就会出现问题。更改为法语没问题,但是当在同一页面中更改为德语时,我收到 404 错误。这也是 Google 网站管理员工具中的生成 404 错误,当我尝试获取此链接并将其粘贴到浏览器中时,它也会出现 404 错误。我在 Google 网站管理员工具中有大约 50 个产品和大约 550 404 错误。我明白问题出在我所描述的内容上。

此外,我有一个 SEO 问题,因为我有这个法语页面:

http://cissmarket.com/ de/cartouches-refilables.html

当我切换到该网站的德语版本时,它会将我带到此链接

http://cissmarket.com/de/cartouches-refilables.html?___from_store=fr< /a> (如果我现在尝试切换到英国,我将得到上面提到的 404)

而不是转到这个:

http://cissmarket.com/de/nachfullpatronen.html

已检查此 在类别中的商店之间切换时出现 404 错误在 magento 上,但它与我的问题无关。

关于设置:

  • 我使用缓存服务,并且我也对所有内容建立了索引。
  • 我尝试访问的产品或类别可用,并针对所有语言设置为活动状态。
  • 系统>一般>网页> URL 选项 >将商店代码添加到 URL 已设置 是的。
  • 系统>一般>网页>搜索引擎优化>使用网络服务器 重写设置为是。
  • 除了以下内容之外,没有对 .htaccess 文件进行任何其他更改 是系统自己制作的。

综上所述:问题是当我从一个页面切换到另一个页面时,连续两次更改语言和错误的 url 地址给出的 404 错误。

任何建议将不胜感激。

更新:尝试了这个 http://www.activo.com/how-to-avoid-the-___from_store-query-parameter-when-switching-store-views-in-magento 但结果在第一次语言更改时出现 404

编辑 #1:

发现问题:文件 languages.phtml 包含此代码 getCurrentUrl()); ?> 并且实际上根据相应的翻译仅替换了语言代码而不是整个 url。

所以应用到这个

http://cissmarket.com/fr/cartouches-refilables.html

它将返回

http://cissmarket.com/de/cartouches-refilables.html

任何人也是如此知道如何获取商店中其他语言的当前页面对应的 URL?

编辑#2(使用@Vinai 解决方案):

它适用于产品页面,但不适用于类别。

I have a problem with a magento installation. I used Magento ver. 1.5.0.1, community edition to develop this website http://cissmarket.com/.

The problem appears when I change the language from the EU version to French and after that to German. The change to french is ok, but when in the same page i change to German i receive a 404 error. Also this is generation 404 errors in the Google webmaster tools and when i try for example to take this link and paste it in the browser it gives me also a 404 error. I have there some 50 products and ~550 404 errors in Google Webmaster tools. I understand that the problem is from what I described.

Moreover I have a SEO problem since I have this page in french:

http://cissmarket.com/de/cartouches-refilables.html

And when I switch to the german version of the website it takes me to this link

http://cissmarket.com/de/cartouches-refilables.html?___from_store=fr (if i try now to switch to uk I will get the 404 mentioned above)

instead of going to this one:

http://cissmarket.com/de/nachfullpatronen.html

Already checked this 404 error when switching between stores when in a category on magento but it does not relate to my problem.

About settings:

  • I use the caching service and also I did index all the content.
  • The product or category I am trying to access is available and set active for all the languages.
  • System > General > Web > URL options > Add Store Code to Urls is set
    to yes.
  • System > General > Web > Search Engines Optimization > Use Web Server
    Rewrites is set to yes.
  • No other changes has been made to the .htaccess file except for the
    ones that the system itself made.

So to conclude: the problem is the 404 given by 2 succesive changes of the language and the bad url address when I switch from one page to another.

Any suggestions would be appreciated.

UPDATE: tried this http://www.activo.com/how-to-avoid-the-___from_store-query-parameter-when-switching-store-views-in-magento but it results in a 404 at the first language change

Edit #1:

Found the problem: file languages.phtml contained this code <?php echo str_replace ("/fr/","/de/",$_lang->getCurrentUrl()); ?> and actually did replace only the language code and not the whole url according to the corresponding translation.

So applied to this

http://cissmarket.com/fr/cartouches-refilables.html

it will return

http://cissmarket.com/de/cartouches-refilables.html

So does anyone know how to get the corresponding URL of the current page for the other languages available in the store?

Edit #2 (using @Vinai solution):

It works on the product pages but not on the category yet.

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

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

发布评论

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

评论(6

給妳壹絲溫柔 2025-01-08 13:13:25

据我所知,原生 Magento 中没有这样的东西。
也就是说,您可以使用以下代码来获取每个商店的当前页面 URL。

$resource = Mage::getSingleton('core/resource');
$requestPath = Mage::getSingleton('core/url')->escape(
    trim(Mage::app()->getRequest()->getRequestString(), '/')
);

$select = $resource->getConnection('default_read')->select()
    ->from(array('c' => $resource->getTableName('core/url_rewrite')), '')
    ->where('c.request_path=?', $requestPath)
    ->where('c.store_id=?', Mage::app()->getStore()->getId())
    ->joinInner(
        array('t' => $resource->getTableName('core/url_rewrite')),
        "t.category_id=c.category_id AND t.product_id=c.product_id AND t.id_path=c.id_path",
        array('t.store_id', 't.request_path')
    );
$storeUrls = (array) $resource->getConnection('default_read')
    ->fetchPairs($select);

这将为您提供一个数组,其中数组键是商店 ID,数组值是 Magento 基本 URL之后的请求路径,例如,假设您的法国商店的 ID 为 1,而德国商店的 ID 为ID 2,你会得到:

Array
(
    [1] => cartouches-refilables.html
    [2] => nachfullpatronen.html
)

然后,在输出每个商店的 URL 的 foreach 循环中,使用

<?php $url = isset($storeUrls[$_lang->getId()]) ? $_lang->getUrl($storeUrls[$_lang->getId()]) : $_lang->getCurrentUrl() ?>

$_lang->getUrl() 的调用将添加基本 URL,因此您将获得每个商店的完整 URL(例如 http://cissmarket.com/de/nachfullpatronen.html)。如果在 core_url_rewrite 表中找不到商店视图值,它将恢复为默认行为。

您仍然需要 ___store=fr 查询参数,否则 Magento 会认为您正在尝试在旧商店的上下文中访问新路径。幸运的是,getUrl() 调用商店模型会自动为您添加该内容。

查询数据库的代码当然可以在任何地方(因为它是 PHP),甚至在模板中,但请不要把它放在那里。 访问数据库的代码的正确位置是资源模型。我建议您创建一个资源模型并将其放入其中的方法中。

There is no such thing in the native Magento as far as I know.
That said, you can use the following code to get the current page URL for each store.

$resource = Mage::getSingleton('core/resource');
$requestPath = Mage::getSingleton('core/url')->escape(
    trim(Mage::app()->getRequest()->getRequestString(), '/')
);

$select = $resource->getConnection('default_read')->select()
    ->from(array('c' => $resource->getTableName('core/url_rewrite')), '')
    ->where('c.request_path=?', $requestPath)
    ->where('c.store_id=?', Mage::app()->getStore()->getId())
    ->joinInner(
        array('t' => $resource->getTableName('core/url_rewrite')),
        "t.category_id=c.category_id AND t.product_id=c.product_id AND t.id_path=c.id_path",
        array('t.store_id', 't.request_path')
    );
$storeUrls = (array) $resource->getConnection('default_read')
    ->fetchPairs($select);

This will give you an array with the array key being the store IDs and the array values being the request path after the Magento base URL, e.g. assuming your French store has the ID 1 and the German one has the ID 2, you would get:

Array
(
    [1] => cartouches-refilables.html
    [2] => nachfullpatronen.html
)

Then, in the foreach loop where the URL for each store is output, use

<?php $url = isset($storeUrls[$_lang->getId()]) ? $_lang->getUrl($storeUrls[$_lang->getId()]) : $_lang->getCurrentUrl() ?>

The call to $_lang->getUrl() will add the base URL, so you will get the full URL for each store (e.g. http://cissmarket.com/de/nachfullpatronen.html). If no store view value is found in the core_url_rewrite table it will revert to the default behaviour.

You still need the ___store=fr query parameter because otherwise Magento will think you are trying to access the new path in the context of the old store. Luckily, the getUrl() call an the store model adds that for you automatically.

The code querying the database can be anywhere of course (since its PHP), even in the template, but please don't put it there. The correct place to have code that access the database is a resource model. I suggest you create a resource model and put it in a method there.

情愿 2025-01-08 13:13:25

我发现了一个丑陋的补丁,直到出现更好的方法。

在管理部分,我在 CMS > 中的所见即所得内添加了以下 JavaScript页数> (我的 404 页面)(所见即所得的开头):

<script type="text/javascript" language="javascript">// <![CDATA[
var lang = "en";
var rooturl = "{{config path="web/unsecure/base_url"}}"
var url = document.location.href;
if(!(url.match("/"+lang+"/")))
{
  var newUrl = url.replace(rooturl , rooturl+lang+"/" );
  window.location.href = newUrl;
}
// ]]></script>


(注意:您需要对所有翻译的 404 页面执行此操作。在每个 404 页面中,您需要修改 lang="en" 的 storeview url 值)

因为 wysiwyg ( tiny_mce) 不允许 javascript 受到威胁,您必须​​修改 js/mage/adminhtml/wysiwyg/tinymce/setup.js。在第 97 行(“var settings =”下)添加以下代码:

extended_valid_elements : 'script[language|type|src]',

I've found an ugly patch until a better approach comes up.

In the admin section, i've added the following javascript inside the wysiwyg in CMS > PAGES > (My 404 pages) (at the beginning of the wysiwyg) :

<script type="text/javascript" language="javascript">// <![CDATA[
var lang = "en";
var rooturl = "{{config path="web/unsecure/base_url"}}"
var url = document.location.href;
if(!(url.match("/"+lang+"/")))
{
  var newUrl = url.replace(rooturl , rooturl+lang+"/" );
  window.location.href = newUrl;
}
// ]]></script>


(Note: you need to do this for all of your translated 404 pages. In each 404 page you need to modify lang="en" for your storeview url value)

Because the wysiwyg (tiny_mce) does not allow javascript to be threated, you'll have to modify js/mage/adminhtml/wysiwyg/tinymce/setup.js. Add the following code under line 97 (under "var settings = "):

extended_valid_elements : 'script[language|type|src]',
带上头具痛哭 2025-01-08 13:13:25

对于 Magento 1.7.0.2 和 1.8.0.0,这是错误修复:

从 /app/code/core/Mage/Core/Model/Url/Rewrite.php 的第 251 行开始

Mage::app()->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $currentStore->getCode(), true);

// endur 02-03-2013 fix for missed store code

// $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();

if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()>getCode()) { 
    $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getRequestPath();
    } else {
      $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();
    }

// endur 02-03-2013 end

确保在以下位置创建文件的自定义副本:
/app/code/local/Mage/Core/Model/Url/Rewrite.php 或:
/app/code/local/YourTheme/Mage/Core/Model/Url/Rewrite.php

来源:

For Magento 1.7.0.2 and 1.8.0.0 this is the bugfix:

Starting from line 251 of /app/code/core/Mage/Core/Model/Url/Rewrite.php

:

Mage::app()->getCookie()->set(Mage_Core_Model_Store::COOKIE_NAME, $currentStore->getCode(), true);

// endur 02-03-2013 fix for missed store code

// $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();

if (Mage::getStoreConfig('web/url/use_store') && $storeCode = Mage::app()->getStore()>getCode()) { 
    $targetUrl = $request->getBaseUrl(). '/' . $storeCode . '/' .$this->getRequestPath();
    } else {
      $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();
    }

// endur 02-03-2013 end

Make sure to create a custom copy of the file in:
/app/code/local/Mage/Core/Model/Url/Rewrite.php or:
/app/code/local/YourTheme/Mage/Core/Model/Url/Rewrite.php

source:

酒中人 2025-01-08 13:13:25

它看起来像是 Magento 1.7 中的一个错误。这是一个对我有用的技巧。
它应该适用于两种语言商店,其商店代码

位于 var/www/html/shop1/app/code/core/Mage/Core/Model/Url/Rewrite.php 中的

URL 中,删除此行

 // $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();

并添加以下内容:

 $storecode = Mage::app()->getStore()->getCode();
 if ($storecode='en')
 {
    $targetUrl = $request->getBaseUrl(). '/'.$storecode.'/' . $this->getRequestPath();
 }
 else
 {
    $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();
 }

It looks like a bug in Magento 1.7. Here is a hack that worked for me.
It should work for a two language store with store code in URL

in var/www/html/shop1/app/code/core/Mage/Core/Model/Url/Rewrite.php

remove this line

 // $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();

and add these:

 $storecode = Mage::app()->getStore()->getCode();
 if ($storecode='en')
 {
    $targetUrl = $request->getBaseUrl(). '/'.$storecode.'/' . $this->getRequestPath();
 }
 else
 {
    $targetUrl = $request->getBaseUrl(). '/' . $this->getRequestPath();
 }
世界和平 2025-01-08 13:13:25

这是这个问题的另一个解决方案。只需在“$this->load($pathInfo, 'request_path');”之后添加此代码即可在应用程序/代码/核心/法师/核心/模型/网址/Rewrite.php:

        if (!$this->getId() && !isset($_GET['___from_store'])) {
            $db = Mage::getSingleton('core/resource')->getConnection('default_read');
            $result = $db->query('select store_id from core_url_rewrite WHERE request_path = "' . $pathInfo . '"');
            if ($result) {
                $storeIds = array();
                if($row = $result->fetch(PDO::FETCH_ASSOC)) {
                    $storeId = $row['store_id'];
                    $storeCode = Mage::app()->getStore($storeId)->getCode();

                    header("HTTP/1.1 301 Moved Permanently");
                    header("Location: http://" . $_SERVER['HTTP_HOST'] . "/" . $pathInfo . "?___store=" . $storeCode);
                    exit();
                }
            }
        }

Here a another solution for this problem. Just add this code after "$this->load($pathInfo, 'request_path');" in app/code/core/Mage/Core/Model/Url/Rewrite.php:

        if (!$this->getId() && !isset($_GET['___from_store'])) {
            $db = Mage::getSingleton('core/resource')->getConnection('default_read');
            $result = $db->query('select store_id from core_url_rewrite WHERE request_path = "' . $pathInfo . '"');
            if ($result) {
                $storeIds = array();
                if($row = $result->fetch(PDO::FETCH_ASSOC)) {
                    $storeId = $row['store_id'];
                    $storeCode = Mage::app()->getStore($storeId)->getCode();

                    header("HTTP/1.1 301 Moved Permanently");
                    header("Location: http://" . $_SERVER['HTTP_HOST'] . "/" . $pathInfo . "?___store=" . $storeCode);
                    exit();
                }
            }
        }
不忘初心 2025-01-08 13:13:25

伙计们。对于这个错误有magento模块。它重写了2个模型
http://www.magentocommerce.com/magento -connect/fix-404-error-in-language-switching.html

guys. For this error there is magento module. It have rewrite 2 models
http://www.magentocommerce.com/magento-connect/fix-404-error-in-language-switching.html

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