【问题标题】:Using PHP to generate rel canonical on multiple pages within the same category (and using the glob( ) function)使用 PHP 在同一类别内的多个页面上生成 rel canonical(并使用 glob() 函数)
【发布时间】:2013-04-10 23:43:29
【问题描述】:

假设您为一家拥有相当大的电子商务网站的公司工作,该网站包含许多产品类别和子类别(有时还有子子类别)。因此,为了方便用户,有些类别具有重复的子类别和重复的内容。每当用户登陆这些重复类别之一并将其指向对 SEO 更友好的类别时,您想使用 PHP 生成 rel 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']).'" />'; ?>

它有效,但它很混乱。我宁愿用一行代码检查 item-1.html - item4.html 而不是四次检查。有谁知道如何实现这一目标?

另外,请记住 item_1.html - item_4.html 不是“随机东西”中唯一的东西,它们只是与“各种东西”共享的重复类别。谢谢!!

更新:

Marty 建议使用glob() 函数循环遍历目录中的所有文件并仅回显我需要的内容。这是我想出的代码:

$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 '<link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" />';
   }
}

这似乎仍然不起作用。谁能进一步照亮我?我从根本上误解了这里的东西吗?

【问题讨论】:

  • 听说过mod_rewrite?对用户来说可能更容易,但如有必要,请使用glob()-函数获取包含某个目录中所有文件名的数组。遍历该数组,以呼应相对链接“et voila”。
  • 嘿@Marty-McVry,感谢您的回复。我会关注mod_rewriteglob() 的唯一问题是,在现实生活中,与我在这里的示例不同,文件名中实际上没有模式。有没有你能想到的解决方法?
  • glob('*') 获取当前工作目录中的所有文件名,无论名称是什么。
  • 如果我使用 glob(*) 我将如何消除我不想分配 rel 规范的文件名?对不起,如果我很密集,我还是个新手。
  • 顺便说一下,这是我尝试过的:$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 '&lt;link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" /&gt;'; } } 无济于事......有什么想法吗?

标签: php glob seo rel


【解决方案1】:

这里有一个更好的解决方案:- 抱歉,顺便说一下,我理解你的问题是错误的 -

// 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 . '" />';
}

【讨论】:

    猜你喜欢
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2013-12-19
    相关资源
    最近更新 更多