8

For our Magento webshop (1.7.0.2), we're trying to optimize the URL structure. We've already managed to improve some things, but in one things we didn't succeed. We want to use a full URL (including categories) for all of our products. For example:

  • www.website.com/products/best-products/product
  • or: www.website.com/products/best-products for a category page

This works fine when browsing through the categories and using the breadcrumbs, but not when searching. It then always uses www.website.com/product as URL, most likely because a product can be in multiple categories and it doesn't know which category to use in the search results.

Is there a way to force this? To make it somehow use the "first" category, or even make it use some default category that you can pick? I'm a developer, so I'm willing to make changes to the code, but only if there isn't another/more reliable way. Any tips or tricks are welcome, many thanks in advance!

4 Answers 4

4

What you want can't be achieved - afaik.

The problem is, that magento is planned with "product in many categories". The problem now is magento doesn't prefer any category and instead it shows no category at all.

The categories are used, when you click throught the categories, this is the only place (afaik) where the category rewrites are used.

But I think you can achieve what you want, by hacking a little bit the rewrite system and just make sure to load a category rewrite and not the one without all categories.

The next problem then is, that you have to make sure, that google still gets every time the same canonical url or you'll get a problem with duplicate content...

And the last think I have in mind: I'm not sure, wether this is a good idea from a SEO point of view. I'm no seo expert, but some one said to me, the shorter the url, the better...

1

Product Url is printed from:
Mage_Catalog_Model_Product::getProductUrl
that method is checking if the URL must include the category using Mage::registry('current_category')

But collections normally load the url rewrites in a unique query (check method Mage_Catalog_Model_Resource_Product_Collection::addUrlRewrite($categoryId = '') )
And then Mage_Catalog_Model_Product::getProductUrl uses that 'request_path" from the $product data.

So a possible trick could be something like this:

        // if we are in search results
    if( ! Mage::registry('current_category')){
        $myDesiredCategoryId = current($product->getCategoryIds()); // you could just use the first category registered with this product or define some other strategy
        $canonicalCategory = Mage::getModel('catalog/category')->load($myDesiredCategoryId);
        Mage::register('current_category', $canonicalCategory);
        $product->setRequestPath(null);
        $productUrlWithCategory = $product->getUrlModel()->getUrl($product);
        Mage::unregister('current_category');
    }

This is not the fastest way (every product in your collection now needs to load the URL rewrite in a separated query) but maybe the easiest if you are looking for a quick solution

1

with Magento 2. We have this option on the backend where you can setup instead of modifying the url routing

-1

I am fairly sure what you need can be achieved by the system config setting Use Categories Path For Product URLs. This can be found under System->Configuration->Catalog->Catalog->Search Engin Optimization. After setting this I think you will need to reindex.

Hope this helps.

1
  • Nope, this setting only works if the current category is set (hence, if you are opening the product from a category page).
    – Simon
    Commented Mar 30, 2016 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.