You are here:  » Add category in urls

Support Forum



Add category in urls

Submitted by magnaromagna on Tue, 2010-06-22 13:45 in

Hello,
Maybe is already posted but cannot find. Is it possible to have the url like

site.ext/category/product-name.html

I mean add the category in the url?

Thanks!

Submitted by support on Tue, 2010-06-22 14:15

Hi,

Sure - it's actually straight forward but I know you've been a Price Tapestry user for several years so i'll give instructions for both distributions.

In both case, in .htaccess first of all CUT line 7:

RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

...and then move it (PASTE) into the END of the file, edited as follows:

RewriteRule ^(.*)/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

It is important that this rule goes last so that it is not confused with the category/merchant/brand index pages.

Original Distribution (01/06A)

Look for the following code beginning at line 166 of search.php:

        $searchresults["products"][$k]["productHREF"] = "product/".tapestry_hyphenate($product["name"]).".html";
        if ($rewrite) $searchresults["products"][$k]["productHREF"] = "../../".$searchresults["products"][$k]["productHREF"];

...and REPLACE with:

        $searchresults["products"][$k]["productHREF"] = $config_baseHREF.tapestry_hyphenate($product["category"])."/".tapestry_hyphenate($product["name"]).".html";

In reviews.php, look for the following code at line 47:

          $rows[$k]["productHREF"] = "../product/".tapestry_hyphenate($row["name"]).".html";

...and REPLACE with:

          $rows[$k]["productHREF"] = $config_baseHREF.tapestry_hyphenate($row["category"])."/".tapestry_hyphenate($row["name"]).".html";

In sitemap.php, look for the following code at line 16:

    $sql = "SELECT name FROM `".$config_databaseTablePrefix."products` WHERE merchant='".database_safe($_GET["merchant"])."' LIMIT ".$start.",".$limit;

...and REPLACE with:

    $sql = "SELECT name,category FROM `".$config_databaseTablePrefix."products` WHERE merchant='".database_safe($_GET["merchant"])."' LIMIT ".$start.",".$limit;

and finally look for the following code at line 32:

        $sitemapHREF = "product/".urlencode(str_replace(" ","-",$row["name"])).".html";

...and REPLACE with:

        $sitemapHREF = urlencode(str_replace(" ","-",$row["category"]))."/".urlencode(str_replace(" ","-",$row["name"])).".html";

Latest Distribution (11/09A)

Look for the following code beginning at line 272 of search.php:

        $searchresults["products"][$k]["productHREF"] = $config_baseHREF."product/".tapestry_hyphenate($product["normalised_name"]).".html";

...and REPLACE with:

        $searchresults["products"][$k]["productHREF"] = $config_baseHREF.tapestry_hyphenate($product["category"])"/".tapestry_hyphenate($product["normalised_name"]).".html";

In reviews.php, look for the following code at line 47:

          $rows[$k]["productHREF"] = $config_baseHREF."product/".tapestry_hyphenate($row["normalised_name"]).".html";

...and REPLACE with:

          $rows[$k]["productHREF"] = $config_baseHREF.tapestry_hyphenate($row["category"])."/".tapestry_hyphenate($row["normalised_name"]).".html";

In sitemap.php, look for the following code at line 16:

    $sql = "SELECT normalised_name FROM `".$config_databaseTablePrefix."products` WHERE filename='".database_safe($_GET["filename"])."' LIMIT ".$start.",".$limit;

...and REPLACE with:

    $sql = "SELECT normalised_name,category FROM `".$config_databaseTablePrefix."products` WHERE filename='".database_safe($_GET["filename"])."' LIMIT ".$start.",".$limit;

and finally look for the following code at line 32:

        $sitemapHREF = "product/".urlencode(str_replace(" ","-",$row["normalised_name"])).".html";

...and REPLACE with:

        $sitemapHREF = urlencode(str_replace(" ","-",$row["category"]))."/".urlencode(str_replace(" ","-",$row["normalised_name"])).".html";

Hope this helps!

Cheers,
David.

Submitted by affiliben on Sat, 2011-02-12 14:15

Hi David
Sorry for the double post, please edit/ delete which one you want.
I am trying to use the category in the product url and have it all lower case.

I have got this almost working for 12/10A by using the .htaccess mod and by modifying tapestry.php with
return $config_baseHREF.(strtolower(tapestry_hyphenate($product["category"])))."/".urlencode(strtolower(tapestry_hyphenate($product["normalised_name"]))).".html";

I have also done the sitemap.php
$sitemapHREF = urlencode(strtolower(str_replace(" ","-",$row["category"])))."/".urlencode(strtolower(str_replace(" ","-",$row["normalised_name"]))).".html";

The urls are looking like I want them to on the browser status bar / source, but when I click on the on the link
the product is not found. Am I missing one fix or my syntax/code wrong?

Thanks
Ben

Submitted by support on Sat, 2011-02-12 15:26

Hi Ben,

If using the category name in place of /product/ which it looks like
you are doing from the mod to includes/tapestry.php (which looks fine)
The corresponding change in .htaccess would also need to include
checks for the file / directory not existing. To do this, look
for the existing /product/ rule at line 7 as follows:

RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

...and move this line to the very end of your .htaccess, modified as follows;

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

Cheers,
David.
--
PriceTapestry.com

Submitted by affiliben on Tue, 2011-02-15 09:54

Hi David
Thank you for the reply.
Unfortunately, I have tried that and it still does not work.
The browser status bar is showing correctly i.e
http://www.mydomain/ptdirectory/category/product.html , ( I am using with with a cms) but for some reason the .htaccess is not finding it and the product page is not found.
As soon as I flip back to the normal settings without the category in the url it works fine. Is there anything else you can suggest? I enclose the code I am using in includes/tapestry.php in format below
Thanks for your help.
Ben

  return $config_baseHREF.urlencode(strtolower(tapestry_hyphenate($product["category"])))."/".urlencode(strtolower(tapestry_hyphenate($product["normalised_name"]))).".html";
    }

Submitted by support on Tue, 2011-02-15 10:14

Hi Ben,

I just spotted an error in the modification to .htaccess that I posted above,
in place of:

RewriteRule ^(.*)/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L]

...this should be:

RewriteRule ^(.*)/(.*).html$ products.php?q=$2&rewrite=1&%{QUERY_STRING} [L]

(using $1 would have meant it was passing the category name as the product
name which wouldn't have worked!)

Cheers,
David.
--
PriceTapestry.com

Submitted by affiliben on Tue, 2011-02-15 10:22

Hi David
Works a treat.
Thank you so much for your excellent help as always.
Ben

Submitted by affiliben on Thu, 2011-02-17 12:01

Sorry to bother you on this one again, but the only urls that are not working are the featured and related.
They are both showing
mydomain.com/ptdirectory//productname.html

I assume I have to edit html/related.php and html/featured.php and change or add something similar to search.php which has

$searchresults["products"][$k]["productHREF"] = tapestry_productHREF($product);,

or is there a way to edit
<?php print $product["productHREF"]; ?>
to something like
<?php print $product["tapestry_productHREF"]; ?>

I have tried that and it does not work.
Thanks
Ben

Submitted by support on Thu, 2011-02-17 13:45

Hi Ben,

Both related and featured products in the latest version use the call to tapestry_productHREF() but it's possible that you are using modifications that don't...

Rather than referring to the html/ files, the relevant code is actually in index.php (featured products) and products.php (related products). In index.php, check for this line:

        $featured["products"][$k]["productHREF"] = $config_baseHREF."product/".tapestry_hyphenate($product["normalised_name"]).".html";

...and REPLACE with...

        $featured["products"][$k]["productHREF"] = tapestry_productHREF($product);

And in products.php, check for

          $searchresults["products"][$k]["productHREF"] = $config_baseHREF."product/".tapestry_hyphenate($related["normalised_name"]).".html";

...and REPLACE with:

          $searchresults["products"][$k]["productHREF"] = tapestry_productHREF($related);

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by affiliben on Thu, 2011-02-17 15:12

Hi David
Your right. I have been trying to update and managed to combine the old and the new incorrectly.
Thanks for the brilliant support as always
Ben