Magento : how to set canonical URL for product?

标签 magento seo canonical-link

我使用的是 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会让你选择:

  1. 最长的 URL(包含最大字符数的 URL)
  2. 具有最大类别深度级别的 URL
  3. 具有最低类别深度级别的非根 URL(并且至少具有
  4. 具有最少字符数(和至少一个类别)的非根 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/

相关文章:

zend-framework - 迁移Magento后的问题

Joomla 网页rel ="canonical"

http - HTTPS 页面上的 Magento HTTP Canonical 标签

wordpress - 如何在 wordpress 类别/存档页面中添加规范标签?

zend-framework - 如何在 Magento 查询中添加 "order by"

php - PayPal 网关已拒绝请求。安全 header 无效 (#10002 : Security error Magento

.htaccess - 更多 HtAccess 重写规则

magento - 通过 Varien_Event_Observer 访问模型

css - 如何在 Magento accessshop 主题 UI 中进行更改

apache - Htaccess 否定