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关掉
2.写一个自定义函数,把站内所有包含有$product->getProductUrl()这个函数去掉,换成自己的函数。该函数如下(有个插件就是这样实现的,感觉不太干净)
3.在网上找到的修改Collection.php的方法(
app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php
),注释掉where('category_id=? OR category_id is NULL')这一行
原来magento在生成URL时,自定义URL排第1,如没有自定义URL,则找一下当前页是否在某分类下,如category_id为空,则直接生成http://xxx.xxx.xxx/产品名.html,如果在某分类下,则按category_id=x对url_rewrite表进行检索,找到相应的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。
不过这种方法有局限性,当一个产品属于多个分类就不行了。
相关文章推荐
- Magento 后台订单详情页Items Ordered列表处,添加跳转到编辑产品的url
- Magento搜索产品结果不精准的问题
- Magento搜索产品结果不精准的问题
- Magento后台订单页面显示产品url地址
- 解决magento保存产品时耗时很长的问题
- Magento搜索产品结果不精准的问题
- [Magento SQL]查询出可配置产品(父产品)、子产品的价格,和与子产品对应属性的差价
- Magento搜索产品结果不精准的问题
- Magento搜索产品结果不精准的问题
- 解决magento保存产品时耗时很长的问题
- Magento导入产品图片名称问题处理
- magento 后台保存分类失败,无法保存产品,ajax一直在转的问题 的解决 - Cannot edit categories in Magento
- Magento url 问题
- Magento搜索产品结果不精准的问题
- Magento Rewrite Url 设置无效问题
- Magento搜索产品结果不精准的问题
- Magento搜索产品结果不精准的问题
- 解决magento添加产品在前台不显示问题
- Magento搜索产品结果不精准的问题
- SpringMVC 中 url-patter 与 @RequestMapping 的对应问题