Вопрос

I have had some SEO feed back on a site I am developing for a client.

Basically the site is indexing both http and https pages.

I have turned on Canonical tags in the back end. To remove duplication it has been recommended that we remove the Canonical tags referencing the https pages and replace the with the corresponding http Canonical tags.

Is this a built in feature in Magento, or am I going to have to create my own module, check the page request type and insert the tag that way?

Это было полезно?

Решение

As I was just extending the Mage_Catalog_Block_category_View I only needed the _prepareLayout function. My Code is below

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;
}

Другие советы

By default Canonical tags should still be http although the address bar of your browser is https.

To test this, in the address bar of your browser type in both the https and http version and then view page source for each version and search for canonical tag (href value) in the header section. They should be the same not https.

If you have a issue let me know what version of magento you are using.

To extend the block do

<?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>

Create block app/code/local/RWS/ProductCanonical/Block/Category/View.php (I extend Mage_Core_Block_Template and copy all the code because when in extend the Mage_Catalog_Block_Category_View it was add multiply tags to the header)

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;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top