HTTPS页面上的Magento HTTP Canonical标签

时间:2012-10-25 10:58:55

标签: http magento https seo canonical-link

我在为客户开发的网站上有一些SEO反馈。

基本上该网站正在索引http和https页面。

我在后端打开了Canonical标签。为了消除重复,建议我们删除引用https页面的Canonical标签,并将其替换为相应的http Canonical标签。

这是Magento的内置功能,还是我必须创建自己的模块,检查页面请求类型并以这种方式插入标记?

2 个答案:

答案 0 :(得分:1)

由于我只是扩展了Mage_Catalog_Block_category_View,我只需要_prepareLayout函数。我的代码在下面

protected function _prepareLayout()
{
parent::_prepareLayout();

$this->getLayout()->createBlock('catalog/breadcrumbs');

if ($headBlock = $this->getLayout()->getBlock('head')) {
  $category = $this->getCurrentCategory();
  if ($title = $category->getMetaTitle()) {
    $headBlock->setTitle($title);
  }
  if ($description = $category->getMetaDescription()) {
    $headBlock->setDescription($description);
  }
  if ($keywords = $category->getMetaKeywords()) {
    $headBlock->setKeywords($keywords);
  }
  if ($this->helper('catalog/category')->canUseCanonicalTag()) {
    if(isset($_SERVER['HTTPS'])) {
      $pattern        = '/((ht){1}(tps://))/';
      $replacement    = 'http://';
      preg_replace($pattern, $replacement, $category->getUrl());
      $headBlock->addLinkRel('canonical', $category->getUrl());
    } else {
      $headBlock->addLinkRel('canonical', $category->getUrl());
    }
  }
  /*
  want to show rss feed in the url
  */
  if ($this->IsRssCatalogEnable() && $this->IsTopCategory()) {
      $title = $this->helper('rss')->__('%s RSS Feed',$this->getCurrentCategory()->getName());
      $headBlock->addItem('rss', $this->getRssLink(), 'title="'.$title.'"');
  }
}

return $this;
}

答案 1 :(得分:0)

默认情况下,尽管浏览器的地址栏为https,但Canonical标签仍应为http。

要对此进行测试,请在浏览器的地址栏中键入https和http版本,然后在每个版本中查看页面源,并在标题部分中搜索规范标记(href值)。它们应该是相同的而不是https。

如果您有任何问题,请告诉我您正在使用的magento版本。

扩展块做

<?xml version="1.0"?>
<config>
    <modules>
        <RWS_ProductCanonical>
            <version>0.1.0</version>
        </RWS_ProductCanonical>
    </modules>

    <global>
        <blocks>
            <productcanonical>
                <class>RWS_ProductCanonical_Block</class>
            </productcanonical>
            <catalog>
                <rewrite>
                    <category_view>RWS_ProductCanonical_Block_Category_View</category_view>
                </rewrite>
            </catalog>
        </blocks> 
        <helpers>
            <productcanonical>
                <class>RWS_ProductCanonical_Helper</class>
            </productcanonical>
        </helpers>
    </global>
</config>

创建块app / code / local / RWS / ProductCanonical / Block / Category / View.php (我扩展Mage_Core_Block_Template并复制所有代码,因为在扩展Mage_Catalog_Block_Category_View时,它会在标题中添加乘法标记)

class RWS_ProductCanonical_Block_Category_View extends Mage_Core_Block_Template
{
    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $this->getLayout()->createBlock('catalog/breadcrumbs');

        if ($headBlock = $this->getLayout()->getBlock('head')) {
            $category = $this->getCurrentCategory();
            if ($title = $category->getMetaTitle()) {
                $headBlock->setTitle($title);
            }
            if ($description = $category->getMetaDescription()) {
                $headBlock->setDescription($description);
            }
            if ($keywords = $category->getMetaKeywords()) {
                $headBlock->setKeywords($keywords);
            }
            if ($this->helper('catalog/category')->canUseCanonicalTag()) {
                ////// add to header here
                $headBlock->addLinkRel('canonical', $category->getUrl() . '?limit=all');
            }
            /*
            want to show rss feed in the url
            */
            if ($this->IsRssCatalogEnable() && $this->IsTopCategory()) {
                $title = $this->helper('rss')->__('%s RSS Feed',$this->getCurrentCategory()->getName());
                $headBlock->addItem('rss', $this->getRssLink(), 'title="'.$title.'"');
            }
        }

        return $this;
    }

    public function IsRssCatalogEnable()
    {
        return Mage::getStoreConfig('rss/catalog/category');
    }

    public function IsTopCategory()
    {
        return $this->getCurrentCategory()->getLevel()==2;
    }

    public function getRssLink()
    {
        return Mage::getUrl('rss/catalog/category',array('cid' => $this->getCurrentCategory()->getId(), 'store_id' => Mage::app()->getStore()->getId()));
    }

    public function getProductListHtml()
    {
        return $this->getChildHtml('product_list');
    }

    /**
     * Retrieve current category model object
     *
     * @return Mage_Catalog_Model_Category
     */
    public function getCurrentCategory()
    {
        if (!$this->hasData('current_category')) {
            $this->setData('current_category', Mage::registry('current_category'));
        }
        return $this->getData('current_category');
    }

    public function getCmsBlockHtml()
    {
        if (!$this->getData('cms_block_html')) {
            $html = $this->getLayout()->createBlock('cms/block')
                ->setBlockId($this->getCurrentCategory()->getLandingPage())
                ->toHtml();
            $this->setData('cms_block_html', $html);
        }
        return $this->getData('cms_block_html');
    }

    /**
     * Check if category display mode is "Products Only"
     * @return bool
     */
    public function isProductMode()
    {
        return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_PRODUCT;
    }

    /**
     * Check if category display mode is "Static Block and Products"
     * @return bool
     */
    public function isMixedMode()
    {
        return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_MIXED;
    }

    /**
     * Check if category display mode is "Static Block Only"
     * For anchor category with applied filter Static Block Only mode not allowed
     *
     * @return bool
     */
    public function isContentMode()
    {
        $category = $this->getCurrentCategory();
        $res = false;
        if ($category->getDisplayMode()==Mage_Catalog_Model_Category::DM_PAGE) {
            $res = true;
            if ($category->getIsAnchor()) {
                $state = Mage::getSingleton('catalog/layer')->getState();
                if ($state && $state->getFilters()) {
                    $res = false;
                }
            }
        }
        return $res;
    }
}