Hyperlink 如何为从同一链接派生的页面添加规范标记?
我使用的是symfony 1.0.6 在我的站点中,我有两个URLHyperlink 如何为从同一链接派生的页面添加规范标记?,hyperlink,symfony1,seo,canonical-link,Hyperlink,Symfony1,Seo,Canonical Link,我使用的是symfony 1.0.6 在我的站点中,我有两个URL http://newe4s.com/news/articles/view/033/job-news-and-information 及 现在,所有的新文章都使用相同的布局,上面的两个链接从数据库中获得相同的数据。 谷歌报告内容重复,因为它为同一内容获取多个URL。 当我搜索一个解决方案时,我发现使用“规范”结构修复了这个需要 <link rel="canonical" href="http://newe4s.com/ne
http://newe4s.com/news/articles/view/033/job-news-and-information
及
现在,所有的新文章都使用相同的布局,上面的两个链接从数据库中获得相同的数据。
谷歌报告内容重复,因为它为同一内容获取多个URL。
当我搜索一个解决方案时,我发现使用“规范”结构修复了这个需要
<link rel="canonical" href="http://newe4s.com/news/articles/view/033/job-news-and-information />
但这里的问题是,两者都使用相同的布局,并且基于文章id(在上面的示例中为033),获取并显示数据。
我无法更改或硬编码canonical href
有没有办法在action.class或模板文件中手动添加链接标记?根据(基于)-哪一点,您可以轻松创建一个帮助程序,将链接标记添加到页面中(例如/lib/helper/CanonicalHelper.php
):
如果您想从操作中添加它,它也会在博客文章中定义
编辑:
如果您创建一个名为CanonicalHelper.php的助手,当您想使用add\u link
函数时,不要忘记将其包括在内:
<?php use_helper('Canonical') ?>
您好,我正在执行以下操作,请告诉我我是对还是错
在/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;
}
?>
Symfony 1.0.11
重要的部分是slot('links')和end_slot()
函数添加链接($href,$rel)
{
插槽(“链接”);
回显“\n\n”;
结束_槽();
}
Hi我在/lib/symfony/helper下创建了一个名为CanonicaHelper.php的帮助程序,并粘贴了包含添加链接的代码。然后在模板中,我调用了add_link()函数,并在layout.php中添加了include_slot('links')。我应该将include_slot('links')重命名为include_slot('Canonical')。。。。我试了两种方法。这不是wortkingHi,我无法在评论框中正确发布我的代码。所以我回答了我自己的问题。请让我知道它是否正确。您将助手放在了错误的文件夹中。在/lib/helper/
中移动CanonicalHelper.php文件(如果文件夹helper不存在,请创建它)。非常抱歉。。是打字错误……)它仅在帮助程序目录中。除此之外,你认为一切都好。。非常感谢……:)嘿我很抱歉。。我总是非常感谢你,男人。。。我刚刚接受了我自己的答案,这就消除了你的偏好。。我再次感到抱歉。。我接受你的答案……没问题,我想你可以删除你的答案,因为那只是一个打字错误;)
/**
* 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'
); ?>
<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>
<?php use_helper('Canonical') ?>
<?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;
}
?>
<?php include_slot('links'); ?>
<?php use_helper('Canonical');?>
<?php echo add_link('nonsense', 'canonical');?>
function add_link($href, $rel)
{
slot('links');
echo "\n<link rel=\"$rel\" href=\"$href\" />\n";
end_slot();
}