如何为来自同一链接的页面添加规范标签?

2023-12-27

我正在使用 symfony 1.0.6。

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

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

and

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

现在,所有新文章都使用相同的布局,上面的两个链接都从数据库中获取相同的数据。 谷歌报告内容重复,因为它获得了相同内容的多个网址。 当我寻找解决方案时,我发现使用“规范”结构可以解决这个问题,这需要

<link rel="canonical" href="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

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

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


根据一张旧票 http://trac.symfony-project.org/ticket/7282(基于旧 symfony 论坛中的旧线程 http://oldforum.symfony-project.org/index.php/t/3778/) - 哪一点到最终来源 http://jnotes.jonasfischer.net/2009/10/symfony-link-tags-e-g-canonical/,您可以轻松创建一个帮助程序,将链接标记添加到您的页面(例如/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);
}

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

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

如果您想从actions,它也在博客文章中定义。

edit:

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

<?php use_helper('Canonical') ?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何为来自同一链接的页面添加规范标签? 的相关文章

随机推荐