title: Using PHP to generate rel canonical on multiple pages within the same category (and using the glob( ) function)
body: <p>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:</p>
  
  <pre><code>&lt;?php if(is_numeric(strpos($_SERVER['REQUEST_URI'],'random-stuff/item_1.html'))) echo '&lt;link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" /&gt;'; ?&gt;
  &lt;?php if(is_numeric(strpos($_SERVER['REQUEST_URI'],'random-stuff/item_2.html'))) echo '&lt;link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" /&gt;'; ?&gt;
  &lt;?php if(is_numeric(strpos($_SERVER['REQUEST_URI'],'random-stuff/item_3.html'))) echo '&lt;link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" /&gt;'; ?&gt;
  &lt;?php if(is_numeric(strpos($_SERVER['REQUEST_URI'],'random-stuff/item_4.html'))) echo '&lt;link rel="canonical" href="http://mydomain.com'.str_replace('random-stuff', 'assorted-things', $_SERVER['REQUEST_URI']).'" /&gt;'; ?&gt;
  </code></pre>
  
  <p>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?</p>
  
  <p>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!!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Marty suggested using the <code>glob()</code> 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:</p>
  
  <pre><code>$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;';
     }
  }
  </code></pre>
  
  <p>This still doesn't seem to work. Can anyone illuminate me further? Am I fundamentally misunderstanding something here?</p>