如何为从同一链接派生的页面添加规范标记? [英] How to add canonical tag the pages that are derived from same link?

查看:135
本文介绍了如何为从同一链接派生的页面添加规范标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用symfony 1.0.6。

I am using symfony 1.0.6.

在我的网站中,我有两个网址。

In my site I have two URLs.

http://newe4s.com/news/articles/view/033/job-news-and-information

http://newe4s.com/news/articles/view/033/job-news-and-information/graduate/Connections-help-graduates-get-jobs

现在,所有新文章都使用相同的布局,上面的链接都从数据库中获取相同的数据。
Google报告重复内容,因为它获取了相同内容的多个网址。
当我搜索解决方案时,我使用规范结构修复了这个问题,需要

Now, all the new articles are using same layout and both above links get same data from database. Google is reporting duplication of contents since it is getting multiple URLs for same content. When I searched for a solution, I got that using "canonical" structure fixes this issue which require

<link rel="canonical" href="http://newe4s.com/news/articles/view/033/job-news-and-information />

将添加到页面的head部分

to be added in head section of page

http://newe4s.com/news/articles/view/033/job-news-and-information/graduate/Connections-help-graduates-get-jobs

但问题是,两者都使用相同的布局并基于文章ID(上例中为033),数据被提取并显示。
我无法更改或硬编码规范href。

But problem here is, both are using same layout and based on article id (033 in above example), data is fetched and displayed. I cannot change or hard-code canonical href.

有没有办法在action.class或模板文件中手动添加链接标记?

Are there any ways of adding link tag manually in action.class or in template file ?

推荐答案

根据旧票(基于 - 哪一点到最终来源,您可以创建一个帮助,为您的页面添加链接标记(例如 /lib/helper/CanonicalHelper.php ):

According to an old ticket (based on an old thread in the old symfony forum) - which point to the final source, you can esaily create an helper that add a link tag to your page (for example /lib/helper/CanonicalHelper.php):

/**
* Add link tag to slot 'links'
*
* @param String $href [Absolute or internat URI]
* @param String $rel [value for 'rel' attribtue, e.g. 'canonical']
*
* @return void
*/
function add_link($href, $rel)
{
  $slot = get_slot('links');

  try {
    $href = url_for($href, true);
    $slot .= "\n<link rel=\"$rel\" href=\"$href\" />";
  } catch (InvalidArgumentException $e) {
    $slot .= "\n<!-- Could not add Link '$href': Only absolute or internal URIs allowed -->";
  }

  slot('links', $slot);
}

然后你可以在你的模板中调用它:

Then you can call it in your template:

<?php add_link(
  'http://newe4s.com/news/articles/view/033/job-news-and-information',
  'canonical'
); ?>

最后,不要忘记在 layout.php中添加插槽

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Title</title>
    <link rel="shortcut icon" href="/favicon.ico" />
    <?php include_javascripts() ?>
    <?php include_stylesheets() ?>
    <?php include_slot('links'); ?>
  </head>

如果你想从行动中添加,它也在博客文章中定义。

If you want to add it from the actions, it is defined in the blog post too.

编辑:

如果你创建一个名为 CanonicalHelper.php 的助手,当你想使用 add_link 函数时,不要忘记包含它:

If you create an helper called CanonicalHelper.php don't forget to include it when you want to use add_link function:

<?php use_helper('Canonical') ?>

这篇关于如何为从同一链接派生的页面添加规范标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆