【问题标题】:How to add canonical tag to OpenCart pages?如何将规范标签添加到 OpenCart 页面?
【发布时间】:2014-03-12 08:11:33
【问题描述】:

我创建了一个新的 OpenCart 1.5.6 商店 - http://www.example.eu - 但是,它的许多页面(就文本上下文而言)与仍在运行的旧商店中的页面相同 - http://www.example.co.uk

将两个网站都编入索引后,我想通过在新网站中包含规范标签来引用旧网站上所有相同的页面:

例如http://www.example.eu/about-us 的“关于我们”页面理想情况下应该在其头部有类似的内容:

<link rel="canonical" href="http://www.example.co.uk/delivery"/>

例如http://www.example.eu/delivery 的“交付”页面理想情况下应该在其头部部分包含类似的内容:

<link rel="canonical" href="http://www.example.co.uk/delivery"/>

我知道这在理论上是如何实现的,但无法在实践中应用。此外,虽然我最关心的是所有信息类型的页面,但理想情况下,我希望能够指定具有规范标签的确切页面,例如:

If (this page is About-Us OR Delivery OR … OR … OR … OR …)
{
Include Canonical Tag of the type <link rel="canonical" href="http://www.example.co.uk/xxxxxxxx"/> , 
where xxxxxxx changes accordingly, depending on the page.
}
Else
{
Don’t include Canonical Tag
}

【问题讨论】:

  • 取决于你的页数,我可以使用in_array()函数。然后,您可以指定数组中的所有页面(url)并传递它。
  • 那么使用301(内容移动)代码从旧页面重定向到新页面怎么样?这可以在.htaccess 文件中实现。

标签: php seo opencart canonical-link


【解决方案1】:

实现这一点的最佳方法是修改您的 Document 类,在您的控制器中设置您的规范,然后在标题控制器中呈现它。

编辑系统/库/document.php

添加:

private $canonical; 到您的类变量顶部。

然后添加以下方法:

public function setCanonical($url) {
    $this->canonical = $url;    
}

public function getCanonical() {
    return $this->canonical;
}

在您的标头控制器中添加:

$this-&gt;data['canonical'] = $this-&gt;document-&gt;getCanonical();

然后您可以在标头控制器中动态设置您的规范:

if (isset($this->request->get['_route_'])):
    $canonical_route = $this->request->get['_route_'];
else:
    $canonical_route = '';
endif;

$this->document->setCanonical($this->data['base'] . $canonical_route);

或者您可以使用 setCanonical 方法在控制器中手动设置它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2013-07-05
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多