How to add a canonical tag to pages derived from a single link?

I am using symfony 1.0.6.

I have two URLs on my site.

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

Now all new articles use the same layout, and both the above links get the same data from the database. Google reports duplicate content as it receives multiple URLs for the same content. When I looked for a solution, I realized that using a "canonical" structure fixes this problem, which requires

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

to be added to the chapter section of the page

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

But the problem is that both use the same layout and based on the article ID (033 in the above example), the data is extracted and displayed. I cannot change or hardcode the canonical href.

action.class ?

+5
3

( symfony) - , , ( /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') ?>
+2

Symfony 1.0.11

- ('links') end_slot(), , , ob_start ob_end()

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

Hi, I am doing as below, and please let me know. If I'm right or wrong.

In / 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;
}
?>

In layout.php:

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

In the Success file:

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

All Articles