您的位置:首页 > 其它

Magento 单产品对应多URL的问题

2010-12-15 08:27 337 查看
今天SEO团队告诉我,Magento中一个产品在站内竟有多个URL.

如在首页,产品A的URL为http://xxx.xxx.xxx/a.html

如果进入到产品A的分类中,其URL则会变成http://xxx.xxx.xxx/分类/a.html

如果进入到产品A的第二级分类下,其URL则会变成http://xxx.xxx.xxx/子分类/a.html

这种情况对SEO极为不利。

以下为三种解决方法:

1.根据Magento可以自定义URL的特性,先把 System->Configuration->Catalog->Search Engine Optimizations中Use categories path for product URLs的属性关掉,而后设置每个产品的URL KEY属性,手工地写成<类别名-产品名>,这样虽然累,但也很灵活。但有个问题,URL如果想做成<类别名/产品名>的形式是不可能的,Magento的formatUrlKey函数已经通过正则,将非字母、数字的信息替换为"-"。如果想实现,可以修改一下app/code/core/Mage/Catalog/Model/Product/Url.php下的urlkey的生成规则,把/加入到非替换列表中。记住:前提条件是把categories
path for product URLs关掉

public function formatUrlKey($str)
{
//$urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str));
$urlKey = preg_replace('#[^0-9a-z/]+#i', '-', Mage::helper('catalog/product_url')->format($str));
$urlKey = strtolower($urlKey);
$urlKey = trim($urlKey, '-');

return $urlKey;
}


2.写一个自定义函数,把站内所有包含有$product->getProductUrl()这个函数去掉,换成自己的函数。该函数如下(有个插件就是这样实现的,感觉不太干净)

public function getHeadProductUrl()
{ $storeId = $this->getStoreId();
$baseUrl = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$product_id = $this->getRequest()->getParam('id'); $headUrl = "";
if (empty($this->_data['urlKey']))
{
$_product = Mage::getModel('catalog/product')->load($product_id);
if($_product->getStatus()==1){ $_categories = $_product->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($_categories[0]);
$headUrl = $baseUrl . $_category->getUrlPath() .'/'. $_product->getUrlPath(); } } $this->_data['urlKey'] = $headUrl;
return $this->_data['urlKey'];
}


3.在网上找到的修改Collection.php的方法(

app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php

),注释掉where('category_id=? OR category_id is NULL')这一行

$select = $this->getConnection()->select() ->from($this->getTable('core/url_rewrite'), array('product_id', 'request_path'))
->where('store_id=?', Mage::app()->getStore()->getId())
->where('is_system=?', 1)
//->where('category_id=? OR category_id is NULL', $this->_urlRewriteCategory)
->where('product_id IN(?)', $productIds)
->order('category_id DESC');


原来magento在生成URL时,自定义URL排第1,如没有自定义URL,则找一下当前页是否在某分类下,如category_id为空,则直接生成http://xxx.xxx.xxx/产品名.html,如果在某分类下,则按category_id=x对url_rewrite表进行检索,找到相应的URL。

不过这种方法有局限性,当一个产品属于多个分类就不行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: