我使用的是 megento 1.7.0.2,有些产品有 3 或 4 个不同的 url:
example.com/category1/product1.html
example.com/category1/category2/product1.html
example.com/prodcut1.html
...
在 Prodcut1 的 HTML 代码中,magento 添加了这个标签:
<link rel="canonical" href="http://example.com/product1.html">
如何选择另一个网址?例如:
<link rel="canonical" href="http://example.com/category1/category2/product1.html">
最佳答案
不幸的是,它无法通过默认的 Magento 功能完成。
首先,您需要决定在定义规范 URL 时要考虑哪些标准。
我们的 Magento SEO extension会让你选择:
- 最长的 URL(包含最大字符数的 URL)
- 具有最大类别深度级别的 URL
- 具有最低类别深度级别的非根 URL(并且至少具有
- 具有最少字符数(和至少一个类别)的非根 URL
这些 URL 也可以添加到 HTML 和 XML 站点地图中。
最重要的是,您可以为任何所选产品单独选择任何 URL 或输入自定义规范。
您也可以自己尝试,使用重写的集合。对于社区版,它看起来像这样:
$collection = Mage::getResourceModel('core/url_rewrite_collection');
$collection->getSelect()->where('product_id = ? AND category_id is not null AND is_system = 1', $productId, Zend_Db::INT_TYPE); <- с наличием категории
$collection->addStoreFilter(Mage::app()->getStore()->getId());
$collection->getSelect()->order(new Zend_Db_Expr('LENGTH(request_path) ' . 'DESC')); <- sorting that depends on the principle of canonical settings.
$rewriteModel = $collection->getFirstItem();
var_dump($rewriteModel); <-damp of the acquired object
然后您需要连接基本商店 URL + “request_path” 属性表单对象 + URL 后缀(如果需要)。
关于Magento : how to set canonical URL for product?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33784975/