【问题标题】:How to add canonical tag the pages that are derived from same link?如何为来自同一链接的页面添加规范标签?
【发布时间】:2012-06-08 10:40:39
【问题描述】:

我正在使用 symfony 1.0.6。

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

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

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

的问题
<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或模板文件中手动添加链接标签?

【问题讨论】:

    标签: hyperlink symfony1 seo canonical-link


    【解决方案1】:

    根据an old ticket(基于old thread in the old symfony forum) - 指向to the final source,您可以轻松地创建一个助手,将链接标签添加到您的页面(例如/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添加它,它也在博客文章中定义。

    编辑:

    如果您创建了一个名为 CanonicalHelper.php 的帮助器,请不要忘记在要使用 add_link 函数时包含它:

    <?php use_helper('Canonical') ?>
    

    【讨论】:

    • 您好,我在 /lib/symfony/helper 下创建了一个助手作为 CanonicaHelper.php,并且我已经粘贴了包含 add_link 的代码。然后在模板中我调用了 add_link() 函数并在 layout.php 中添加了 include_slot('links') 我应该将 include_slot('links') 重命名为 include_slot('Canonical')...。我尝试了两种方式。它不工作
    • 您好,我无法在评论框中正确发布我的代码。所以我回答了我自己的问题。请告诉我是否正确。
    【解决方案2】:

    Symfony 1.0.11

    重要的部分是 slot('links') 和 end_slot(),所以中间的任何打印都将分配给类似于 ob_start 和 ob_end() 的插槽

    function add_link($href, $rel)
       {
          slot('links');
          echo "\n<link rel=\"$rel\" href=\"$href\" />\n";
          end_slot();
       }
    

    【讨论】:

      【解决方案3】:

      您好,我正在做以下事情,如果我是对的或错的,请告诉我。

      在 /lib/symfony/CanonicalHelper.php 中

      <?php 
      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   -->";
      }
       return $slot;
      }
      ?>
      

      在 layout.php 中:

      <?php include_slot('links'); ?>
      

      在成功文件中:

      <?php use_helper('Canonical');?>
      <?php echo add_link('nonsense', 'canonical');?>
      

      【讨论】:

      • 您将帮助程序放在错误的文件夹中。移动 /lib/helper/ 中的 CanonicalHelper.php 文件(如果文件夹帮助程序不存在,则创建它)。
      • 非常抱歉.. 输入错误.. :) 它仅在帮助目录中。除此之外,每个人都认为OK..非常感谢... :)
      • 嘿...我很抱歉..我一直非常感谢你的人...我刚刚接受了我自己的答案,它删除了您的偏好..再次抱歉..我接受你的答案..
      • 没问题,我认为你可以删除你的答案,因为这只是一个打字错误;)
      猜你喜欢
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 2022-11-03
      相关资源
      最近更新 更多