Using PHP to generate rel canonical on multiple pages within the same category (and using the glob( ) function)

StackOverflow https://stackoverflow.com/questions/15938443

  •  03-04-2022
  •  | 
  •  

質問

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?

役に立ちましたか?

解決

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 . '" />';
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top