You are here:  » Category/brand url

Active Forum Topics


Category/brand url

Submitted by marco on Thu, 2015-01-08 10:15 in

Hello,

I have added a "/category/brand/" url in htacces like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/$ search.php?q=category:$1:&brandFilter=$2&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^(.*)/(.*)/(.*).html$ search.php?q=category:$1:&brandFilter=$2&page=$3&rewrite=1&%{QUERY_STRING} [L]

This works, except when the brand consists of more than one word.
for example: /shoes/tommy-hilfiger/

Searchresults displays a "-" in between the words of the brand in the title (and no products are shown).

If i change the "-" in a "+" in the url the results are shown.

How to make this work with the "-" in url?

Best,
Marco

Submitted by support on Thu, 2015-01-08 11:08

Hi Marco,

Filter values aren't normalised prior to search so the "-" remain but you can get the same results using the query category:Category-Name:brand:Brand-Name - have a go with:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/$ search.php?q=category:$1:brand:$2&rewrite=1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*).html$ search.php?q=category:$1:brand:$2&page=$3&rewrite=1&%{QUERY_STRING} [L]

I've included a second instance of the RewriteCond lines, as these only apply to the next RewriteRule immediately following...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by marco on Thu, 2015-01-08 13:15

Thanks!

Submitted by sirmanu on Wed, 2019-01-02 10:09

Hi!

This looks pretty neat for SEO purposes. I want to implement it in 15/09A , but I am a bit lost about how to achieve that.
My site have category hierarchy enabled, so I have a structure like the following URI:

example.com/cat/electronic/tablets

And If some user selects brand Apple, I want to have example.com/cat/electronic/tablets/apple.

How can I get that pretty URL when the user clicks on the search filters?

Submitted by support on Wed, 2019-01-02 15:50

Hi,

It's normally easiest in this case to get the rewrite working as you would like and then to look at the redirection (the filter form submits to search.php so it would need to determine from the filter parameters that it is OK to redirect i.e. no other filters in effect.

Ideally, there needs to be demarcation between the category and brand part for example by using the format;

example.com/category/Electronics/Tablets/brand/Apple/

(making sure the format ends in "/" means that the pagination can work nicely with 2.html, 3.html etc. appended as normal).

To try this in the first instance, if you edit your .htaccess and insert the following rules _before_ the existing category rules (otherwise they would override)

RewriteRule ^category/(.*)/brand/(.*)/$ search.php?q=category:$1:&brandFilter=$2&rewrite=1&%{QUERY_STRING} [L,B]
RewriteRule ^category/(.*)/brand/(.*)/(.*).html$ search.php?q=category:$1:&brandFilter=$2&page=$3&rewrite=1&%{QUERY_STRING} [L,B]

In addition, edit search.php and look for the following code at line 18:

  $brandFilter = (isset($_GET["brandFilter"])?$_GET["brandFilter"]:"");

...and REPLACE with:

  $brandFilter = (isset($_GET["brandFilter"])?tapestry_normalise($_GET["brandFilter"]):"");

With that in place, browse to a /category/ results page, and then append brand/Existing-Brand/ to the URL and the results should be shown with the clean URL and filtered...

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Wed, 2019-01-02 17:08

Ok David, that is working.
I only need to compare with lower case at searchfilters.php because I have some filters for capital letter.

$selected = (strtolower ($brandFilter)==strtolower ($row["brand"])?"selected='selected'":"");

The redirection is okey.

?q=category:electronics/tablets/:&merchantFilter=&brandFilter=apple&minPrice=&maxPrice=

This is URL decoded. I don't know why are three dots before &merchantFilter

Submitted by support on Thu, 2019-01-03 10:03

Hi,

The : comes from the query constructed for the index searches e.g. category:/path/to/category/:

To apply the redirect, edit search.php and look for the following code at line 105:

          $nodeInfo = tapestry_categoryHierarchyNodeInfo($parts[1]);

...and REPLACE with:

          if ((!$rewrite) && ($page==1) && ($brandFilter) && ($merchantFilter.$categoryFilter.$minPrice.$maxPrice===""))
          {
            header("Location: ".tapestry_indexHREF("category",$parts[1])."brand/".tapestry_hyphenate($brandFilter)."/");
            exit();
          }
          $nodeInfo = tapestry_categoryHierarchyNodeInfo($parts[1]);

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Thu, 2019-01-03 20:20

Thanks David. It works perfectly.

Submitted by sirmanu on Tue, 2019-01-08 16:02

This starts to be interesting!

From other thread, I have the ability to add custom description to be displayed under
example.com/cat/electronic/tablets/. (I have a column description at pt_categories_hierarchy).

Is any chance I can have a different text depending on the brand selected?

For instance, if the user selects apple: example.com/cat/electronic/tablets/brand/apple.

Thank you!

Submitted by support on Wed, 2019-01-09 08:52

Sure,

If you haven't done so already, apply the mods to add description field to Brand Mapping (described in this comment) and then where you are displaying the description from Category Hierarchy Mapping, add for example:

  if ($rewrite && $brandFilter)
  {
    $sql = "SELECT description FROM `".$config_databaseTablePrefix."brands` WHERE name='".database_safe($brandFilter)."' AND description <> ''";
    if (database_querySelect($sql,$rows))
    {
      print "<p>".$rows[0]["description"]."</p>";
    }
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Wed, 2019-01-09 18:10

The problem with that approach is for example, if I have the brand Apple, the description should be different for tablets or mobiles phones.
How can I achieve that?

Submitted by support on Thu, 2019-01-10 11:17

Hi,

What you could do is simply create a directory hierarchy (relative to Price Tapestry installation folder) that exactly matches that displayed in the URL, containing a file "info.html". For example, for

http://www.example.com/category/Portable-Electronics/Tablets/brand/Apple/

Create the file:

category/Portable-Electronics/Tablets/brand/Apple/info.html

And then in your code, use:

  if ($rewrite && $brandFilter)
  {
    $path = str_replace($config_baseHREF,"",$_SERVER["REQUEST_URI"]);
    $filename = $path."info.html";
    if (file_exists($filename))
    {
      require($filename);
    }
  }

...and the contents of info.html will be included into the page.

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Wed, 2022-01-12 14:22

Hey David,
I've been looking to do something like this on my installation, though my filter setup on-screen is a tickbox in the sidebar and I tried the above code and it didn't work for me.

Here's what's in my search.php

$brandFilter = ($_GET["brandFilter"]?$_GET["brandFilter"]:array());
  if (($k=array_search("",$brandFilter))!==FALSE) unset($brandFilter[$k]);

I tried the below but that didn't work either

$brandFilter = ($_GET["brandFilter"]?tapestry_normalise(?$_GET["brandFilter"]:array());

Submitted by support on Thu, 2022-01-13 11:17

Hi,

For the checkbox version the brandFilter parameter in the URL needs to use [] to present the value to PHP as an array; so the .htaccess code would be:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/$ search.php?q=category:$1:&brandFilter[]=$2&rewrite=1&%{QUERY_STRING} [L]
RewriteRule ^(.*)/(.*)/(.*).html$ search.php?q=category:$1:&brandFilter[]=$2&page=$3&rewrite=1&%{QUERY_STRING} [L]

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Thu, 2022-01-13 12:40

Thanks David,
What code would I need to put into my search.php file please? I'm running 15/09A, using category hierarchy menu with the checkbox filter. I tried using [] instead of () but it caused an error.

Submitted by support on Thu, 2022-01-13 15:10

Hi,

What code do you currently have for $categoryFilter in search.php - if you already have checkbox filtering set-up it _shouldn't_ need to be changed at all for this...?

(or in other words; with the new RewriteRule-s in place what is not working on the search results page?)

Thanks,
David.
--
PriceTapestry.com

Submitted by bat on Thu, 2022-01-13 15:33

Using the .htaccess code you provided and leaving my search.php as-is, no products show up in the categories.

My search.php filter is

$rewrite = isset($_GET["rewrite"]);
  $merchantFilter = ($_GET["merchantFilter"]?$_GET["merchantFilter"]:array());
  if (($k=array_search("",$merchantFilter))!==FALSE) unset($merchantFilter[$k]);
  $categoryFilter = ($_GET["categoryFilter"]?$_GET["categoryFilter"]:array());
  if (($k=array_search("",$categoryFilter))!==FALSE) unset($categoryFilter[$k]);
  $brandFilter = ($_GET["brandFilter"]?$_GET["brandFilter"]:array());
  if (($k=array_search("",$brandFilter))!==FALSE) unset($brandFilter[$k]);
  $colourFilter = ($_GET["colourFilter"]?$_GET["colourFilter"]:array());
  if (($k=array_search("",$colourFilter))!==FALSE) unset($colourFilter[$k]);

Submitted by support on Thu, 2022-01-13 16:01

Ah - you would need the RewriteRule-s from further up the script sorry so that the brand can be differentiated from the category, so using:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/(.*)/brand/(.*)/$ search.php?q=category:$1:&brandFilter[]=$2&rewrite=1&%{QUERY_STRING} [L,B]
RewriteRule ^category/(.*)/brand/(.*)/(.*).html$ search.php?q=category:$1:&brandFilter[]=$2&page=$3&rewrite=1&%{QUERY_STRING} [L,B]

...but this would need to go further up in .htaccess _before_ the main /category/ rewrite rules otherwise they would override...

So this would support e.g;

https://example.com/category/Widgets/Blue-Widgets/brand/WidgetCo/

(and won't need any changes to search.php)

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Thu, 2022-01-13 16:12

Hi David,
I've tried this but it's not rewriting the URLs, they're still the same (although the products are appearing now).
Would I be best to send you my .htaccess and search.php file so you know what's going on in there?

Submitted by support on Thu, 2022-01-13 16:19

Sure - drop them in an email and i'll check it out for you...

Cheers,
David.
--
PriceTapestry.com