0

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?

Kale
  • 601
  • 1
  • 8
  • 25
  • 1
    Heard about [mod_rewrite](http://httpd.apache.org/docs/current/mod/mod_rewrite.html)? Might be easier for the user, but if necessary, use the [`glob()`](http://php.net/manual/en/function.glob.php)-function to get an array containing all the filenames in a certain directory. Iterate through that array echoing the relative links "et voila". – Marty McVry Apr 10 '13 at 23:45
  • Hey @Marty-McVry, thanks for the response. I'll look in to `mod_rewrite`. The only problem w/ `glob()` is that in real life, unlike my example here, there's actually no pattern in the file names. Is there a work around you can think of? – Kale Apr 11 '13 at 16:17
  • `glob('*')` fetches all the filenames in the current working directory, no matter what the name is. – Marty McVry Apr 11 '13 at 16:31
  • If I use `glob(*)` how would I eliminate the file names I don't want to assign rel canonical to? Sorry if I'm being dense, I'm still a novice. – Kale Apr 11 '13 at 16:36
  • By the way, here's what I've tried: `$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 ''; } }` To no avail... any ideas? – Kale Apr 11 '13 at 17:48

1 Answers1

1

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 . '" />';
}
Marty McVry
  • 2,838
  • 1
  • 17
  • 23