2012-02-01 36 views
4

我有一个magento安装问题。我使用Magento版本。 1.5.0.1社区版开发本网站http://cissmarket.com/Magento多语言 - 在语言中的双重变化结果在404(或如何更改商店内的语言而不是视图)

将语言从EU版本更改为法语,然后再更改为德语时出现问题。法国的变化是好的,但是当我在同一页面更改为德语时,我收到了404错误。此外,这是谷歌网站管理员工具中的404代错误,当我尝试例如采取此链接并将其粘贴到浏览器中时,它也给我一个404错误。 Google网站站长工具中有50种产品和〜550 404错误。我知道问题出在我所描述的问题上。

而且我有一个搜索引擎优化的问题,因为我有这个页面在法国:

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

当我切换到网站的德语版需要我这个链接

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

而不是去这个:

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

已经检查了这个404 error when switching between stores when in a category on magento但它与我的问题没有关系。

关于设置:

  • 我使用缓存服务,也可以我做了索引的所有内容。
  • 我试图访问的产品或类别可用,并且对所有语言都处于活动状态。
  • 系统>常规>网址>网址选项>将商店代码添加到网址设置为 为yes。
  • 系统>常规> Web>搜索引擎优化>使用Web服务器 重写设置为yes。
  • 除了系统自己制作的 之外,没有对.htaccess文件进行其他更改。

所以得出结论:问题在于,当我从一个页面切换到另一个页面时,语言和URL地址的2次连续变化给出了404。

任何建议,将不胜感激。

更新:试过这种http://www.activo.com/how-to-avoid-the-___from_store-query-parameter-when-switching-store-views-in-magento但在第一语言变化

编辑#1的结果在404:

发现了问题:文件languages.phtml包含这些代码<?php echo str_replace ("/fr/","/de/",$_lang->getCurrentUrl()); ?>和实际上并更换只根据相应的翻译语言代码而不是整个网址。

因此适用于本

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

它将返回

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

因此,没有人知道如何获得当前的相应网址商店中可用其他语言的页面?

编辑#2(使用@Vinai溶液):

它的工作原理上的产品网页,但没有对类。

回答

7

据我所知,原生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 2,你会得到:

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

然后,在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),即使在模板中,但请不要放在那里。 具有访问数据库的代码的正确位置是资源模型。我建议你创建一个资源模型并把它放在一个方法里面。

+0

我会检查一下,然后回来一个答案!谢谢! – Mike 2012-02-08 10:54:28

+0

非常感谢您的回答。它工作正常,但不在类别页面上,但仅在产品页面上。我使用'print_r($ storeUrls);'在类别页面上尝试了简单版本的输出,而在数组页面上,数组是空的,但是在产品页面上,它会返回正确的数组元素,如上所述。 – Mike 2012-02-10 12:11:22

3

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

在管理部分,我已经添加了所见即所得的内部下面的JavaScript在CMS>页面>(我的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值)

因为所见即所得(tiny_mce)不允许Javascript被threated,你将不得不修改JS /法师/adminhtml/wysiwyg/tinymce/setup.js。在97行(在“var settings =”下)添加以下代码:

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

以这种方式链接到您自己的网站最终可能会将您的帖子删除为垃圾邮件。我正在删除链接,因为没有它你的答案似乎很好。请*不要*重新添加。 – 2012-10-22 15:01:08

0

这里是另一个解决此问题的方法。只需在“$ this-> load($ pathInfo,'request_path');”在应用程序/代码/核心/法师/核心/型号/ 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(); 
       } 
      } 
     } 
+0

此解决方案看起来很有趣。任何人都可以确认它会工作,而不是创建其他问题? – 2013-01-04 13:31:04

0

它看起来像在Magento 1.7的bug。这是一个为我工作的黑客。 应该在URL的两个语言专卖店工作与存储代码

在无功/ www/html等/ shop1 /应用/代码/核心/法师/核心/型号/ URL/Rewrite.php

删除此线

// $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(); 
} 
1

对于Magento的1.7.0.2和1.8.0.0这是修正:

起价线251 /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 

确保在创建该文件的自定义复印: /app/code/local/Mage/Core/Model/Url/Rewrite.php或: /app/code/local/YourTheme/Mage/Core/Model/Url/Rewrite.php

source: