duanjuete9206 2013-04-10 23:43
浏览 50
已采纳

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

Let's say you work for a company that has a reasonably large e-commerce website with lots of product categories and subcategories (and sometimes, sub-subcategories). As such, for the ease of the user, there are some categories that have duplicate subcategories w/ duplicate content. You'd like to use PHP to generate a rel canonical whenever the user lands on one of these duplicate categories and point it towards the more SEO friendly category. In this case, the category w/ duplicate subcategories is "random-stuff" and the category I'd like the canonical to point to is "assorted-things". So, item_1.html - item_4.html are all found in both "random-stuff" and "assorted-things", but I'd like the canonical to point to "assorted-things". At present, here's what I have:

<?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']).'" />'; ?>

It works, but it's a lot of clutter. I'd prefer to have one line of code that checked for item-1.html - item4.html instead of four checks. Does anyone have an idea of how this could be achieved?

ALSO, bare in mind that item_1.html - item_4.html are NOT the only things in "random-stuff", they're just the duplicate categories shared w/ "assorted-things". Thanks!!

UPDATE:

Marty suggested using the glob() function to loop through all files in the directory and echo only what I needed. Here's the code I came up with as a result:

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

This still doesn't seem to work. Can anyone illuminate me further? Am I fundamentally misunderstanding something here?

  • 写回答

1条回答 默认 最新

  • douwang4374 2013-04-11 20:17
    关注

    Here's a better solution: - sorry, by the way, I understood your question wrong -

    // 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 . '" />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向