使用PHP在同一类别的多个页面上生成rel canonical(并使用glob()函数)

使用PHP在同一类别的多个页面上生成rel canonical(并使用glob()函数),php,glob,seo,rel,Php,Glob,Seo,Rel,假设你在一家拥有相当大的电子商务网站的公司工作,该网站有很多产品类别和子类别(有时还有子类别)。因此,为了方便用户,有些类别具有重复的子类别和重复的内容。您希望在用户登陆这些重复类别时使用PHP生成一个rel规范,并将其指向对SEO更友好的类别。在本例中,带有重复子类别的类别是“随机的东西”,我希望canonical指向的类别是“分类的东西”。因此,item_1.html-item_4.html都可以在“随机资料”和“分类资料”中找到,但我想让规范指向“分类资料”。目前,我所拥有的是: <

假设你在一家拥有相当大的电子商务网站的公司工作,该网站有很多产品类别和子类别(有时还有子类别)。因此,为了方便用户,有些类别具有重复的子类别和重复的内容。您希望在用户登陆这些重复类别时使用PHP生成一个rel规范,并将其指向对SEO更友好的类别。在本例中,带有重复子类别的类别是“随机的东西”,我希望canonical指向的类别是“分类的东西”。因此,item_1.html-item_4.html都可以在“随机资料”和“分类资料”中找到,但我想让规范指向“分类资料”。目前,我所拥有的是:

<?php if(is_numeric(strpos($_SERVER['REQUEST_URI'],'random-stuff/item_1.html'))) echo '<link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" />'; ?>
<?php if(is_numeric(strpos($_SERVER['REQUEST_URI'],'random-stuff/item_2.html'))) echo '<link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" />'; ?>
<?php if(is_numeric(strpos($_SERVER['REQUEST_URI'],'random-stuff/item_3.html'))) echo '<link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" />'; ?>
<?php if(is_numeric(strpos($_SERVER['REQUEST_URI'],'random-stuff/item_4.html'))) echo '<link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" />'; ?>

这似乎仍然不起作用。有人能进一步说明我吗?我根本上误解了什么吗?

这里有一个更好的解决方案:-对不起,顺便说一句,我对你的问题理解错了-

// First, get last portion of url
$filename = end(explode('/',$_SERVER['REQUEST_URI']));

// Check if the same filename exists in 'assorted-things' directory:
if (file_exists("../assorted-things/$filename")) {
    // If so, echo canonical
    echo '<link rel="canonical" href="http://mydomain.com/assorted-things/' . $filename . '" />';
}
//首先,获取url的最后一部分
$filename=end(分解('/',$服务器['REQUEST\u URI']);
//检查“分类内容”目录中是否存在相同的文件名:
如果(文件_存在(“../schodled things/$filename”)){
//如果是这样的话,echo canonical
回声';
}

听说过吗?对用户来说可能更容易,但如果需要,可以使用-函数获取包含某个目录中所有文件名的数组。在数组中重复出现相关链接“et voila”。嘿@Marty McVry,谢谢你的回复。我将查看
mod\u rewrite
。唯一的问题是,在现实生活中,与我这里的示例不同,文件名中实际上没有模式。你能想到一个解决办法吗?
glob('*')
获取当前工作目录中的所有文件名,不管名称是什么。如果我使用
glob(*)
我如何消除我不想分配rel canonical的文件名?很抱歉,如果我太笨了,我还是个新手。顺便说一句,这里是我尝试过的:
$dir=http://www.mydomain.com/random-stuff/*';  foreach(glob($dir)as$file){if($file=='item_1.html'|| item_2.html'| |'item_3.html'| |'item_4.html'){echo'}
无效。。。有什么想法吗?
// First, get last portion of url
$filename = end(explode('/',$_SERVER['REQUEST_URI']));

// Check if the same filename exists in 'assorted-things' directory:
if (file_exists("../assorted-things/$filename")) {
    // If so, echo canonical
    echo '<link rel="canonical" href="http://mydomain.com/assorted-things/' . $filename . '" />';
}