You are here:  » Category Hierarchy: 301 with alternatives


Category Hierarchy: 301 with alternatives

Submitted by sirmanu on Mon, 2019-05-27 15:07 in

One of the best features that I think you have developed for PT is Category Hierarchy. Is the best way you can get the structure you want, with a lot of work, of course.

However, as is currently implemented, when the site starts to grow and have some months/years active, some categories names must change because there were wrong, or just the names may differ.

For example. Some years ago, people search for "smartphones", but now, people are getting back to "mobile phones".

So, what I ask is about how to implement an "alternatives" mapping (same that are in other places or PT).

For example. I have a category called smartphones (electronics/smartphones). Next, I rename this category to: electronics/mobile-phones.

What I should do is to add a new alternative field (smartphones) into mobiles phones so when someone arrives to "electronics/smartphones" it gets redirected to "electronics/mobile-phones"

What I have thought is to add a new column for alternatives in pt_categories_hierarchy and create a method for "register alternative categories" so it runs when a category is not found. Currently, all my categories names are unique.

What do you think? Is it feasible to implement?

Thank you very much.

Submitted by support on Tue, 2019-05-28 08:07

Hi,

A quick way to ensure that all possible links to an old category name are redirected to the new one would be via .htaccess. The entries would need to go at the top of the RewriteRule list, immediately after the RewriteBase line for example:

RewriteRule ^category/electronics/smartphones/(.*) category/electronics/movile-phones/$1 [L,R=301]

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Tue, 2019-05-28 08:48

Yep, I am currently doing that, but at the site is growing, this is going to be messy and tend to errors.

Submitted by support on Tue, 2019-05-28 10:43

Ah - in that case, it works out easily enough but test thoroughly of course. First to add new "redirects" column to pt_categories_hierarchy run the following dbmod.php script from the top level folder of the installation:

<?php
  set_time_limit
(0);
  require(
"includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."categories_hierarchy` ADD `redirects` text";
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

Edit admin/categories_hierarchy_configure.php and look for the following code at line 68:

    $sql = "UPDATE `".$config_databaseTablePrefix."categories_hierarchy` SET alternates = '".database_safe($alternates)."' WHERE id='".database_safe($id)."'";

...and REPLACE with:

    $sql = "UPDATE `".$config_databaseTablePrefix."categories_hierarchy` SET alternates = '".database_safe($alternates)."',redirects='".database_safe(trim($_POST["redirects"]))."' WHERE id='".database_safe($id)."'";

And then the following code at line 89:

 widget_textArea("Alternatives","alternates",FALSE,$category["alternates"],200,12);

...and REPLACE with:

 widget_textArea("Alternatives","alternates",FALSE,$category["alternates"],200,12);
  widget_textArea("Redirects","redirects",FALSE,$category["redirects"],100,12);

Finally, edit includes/admin.php and add the following new function to the end of the file:

  function getCategoryPath($id)
  {
    global $config_databaseTablePrefix;
    $categories = array();
    do {
      $sql = "SELECT name,parent FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id = '".$id."'";
      database_querySelect($sql,$rows);
      array_unshift($categories,$rows[0]["name"]);
    } while($id = $rows[0]["parent"]);
    return implode("/",$categories);
  }

...and finally look for the following code at line 435:

      if (!database_querySelect($sql,$rows))

...and REPLACE with:

      if (!database_querySelect($sql,$rows))
      {
        $sql = "SELECT id FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE redirects LIKE '%".database_safe($name)."%' LIMIT 1";
        if (database_querySelect($sql,$rows))
        {
          $path = getCategoryPath($rows[0]["id"]);
          $page = (isset($_GET["page"])?intval($_GET["page"]):1);
          $location = tapestry_indexHREF("category",$path);
          if ($page > 1) $location .= $page.".html";
          header("Location: ".$location,301);
        }
        exit();
      }

This should handle redirects for any base or paginated request to category hierarchy index page for previous names listed in the new Redirects field to the new category name.

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Sat, 2019-06-29 06:51

Hi David. I couldn't tested until now. It is working perfectly.
When you said "...and finally look for the following code at line 435:" I didn't know which file you where referring to. I added the code to categories.php.
Also, for redirect I used:

      header("HTTP/1.1 301 Moved Permanently");
      header("Location: ".$location);