我在为客户开发的网站上收到了一些 SEO 反馈。
基本上,该网站同时索引 http 和 https 页面。
我在后端打开了规范标签。为了消除重复,我们建议我们删除引用 https 页面的规范标签,并将其替换为相应的 http 规范标签。
这是 Magento 的内置功能,还是我必须创建自己的模块,检查页面请求类型并以这种方式插入标签?
我在为客户开发的网站上收到了一些 SEO 反馈。
基本上,该网站同时索引 http 和 https 页面。
我在后端打开了规范标签。为了消除重复,我们建议我们删除引用 https 页面的规范标签,并将其替换为相应的 http 规范标签。
这是 Magento 的内置功能,还是我必须创建自己的模块,检查页面请求类型并以这种方式插入标签?
因为我只是扩展 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;
}
默认情况下,Canonical 标签仍应为 http,尽管您的浏览器的地址栏是 https。
要对此进行测试,请在浏览器的地址栏中输入 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;
}
}