Use Categories in Product URLs for Magento SEO without Duplicate Content

来源:互联网 发布:医院网络销售 编辑:程序博客网 时间:2024/06/05 19:00

Use Categories in Product URLs for Magento SEO without Duplicate Content

Magento Open Source eCommerce logo Magento provides the option to use categories in your product URLs for the search engine optimization benefits it brings, but they’ve implemented it in a very unusual way. Basically, if you enable this option, you’ll end up with at least 4 URLs per product. Once people realize it, this often raises a number of concerns including possible duplicate content penalty from Google, fractured link juice for product pages, frustrated admins, etc.

With so many people also searching for a clever solution, I decided disabling the feature was an unacceptable solution, and took the challenge.

 

First you have to enable the “Use categories path for product URLs” feature under System > Configuration > Catalog > Search Engine Optimization.

Use categories path for product URLs – This determines how the URL Rewrites autogenerate. If you choose Yes, the URL rewrite for products will include the associated category, and a separate rewrite will generate for each associated category. If you choose No, the URL Rewrites will include only the product name, and there will be only one rewrite, regardless of how many categories the product is associated to.

Once this feature is enabled, you’ll be to use any of these URLs to visit the same product page:

  • /catalog/product/view/id/<product_id>
    example: http://www.domain.tld/catalog/product/view/id/6
    Internal to Magento; never actually exposed.

  • /catalog/product/view/id/<product_id>/category/<category_id>
    example: http://www.domain.tld/catalog/product/view/id/6/category/10
    Internal to Magento; never actually exposed.

  • /name-of-product
    example: http://www.domain.tld/super-dee-duper-tent-1000-olive
    Normally used on the front page, in content blocks, or anywhere other than a category page. This is because without being on a category page, Magento doesn’t know which category would be appropriate to display in the URL (since it provides the ability to have multiple categories per product). Therefore it opts to not display any category at all. A bad decision in my opinion.

  • /category-1/sub-category-1/name-of-product
    example: http://www.domain.tld/sporting-goods/camping-hiking/super-dee-duper-tent-1000-olive
    Used from category pages.

  • /category-2/name-of-product
    example: http://www.domain.tld/affordable-housing/super-dee-duper-tent-1000-olive
    Used from category pages.

  • etc.
    Depending on how many categories per product.

Personally, I don’t like my products having multiple URLs. Between the front page and the category pages, two separate URLs are introduced to GoogleBot as it crawls through the pages. Even though you may not be seriously penalized for duplicate content, which page do you want to appear in Google SERPs? Which one do you want your customers to link at? You’re splitting your link juice and causing confusion.

When I saw this feature for the first time, all I really expected was a single URL like:

http://www.domain.tld/sporting-goods/camping-hiking/super-dee-duper-tent-1000-olive

Well, it took about four hours to find, but I finally came up with a patch to Magento core that delivers this result. Check it out below in unified diff / patch file format:


Index: app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php
===================================================================
--- app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php(revision 2102)
+++ app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php(working copy)
@@ -553,7 +553,11 @@
->from($this->getTable('core/url_rewrite'), array('product_id', 'request_path'))
->where('store_id=?', Mage::app()->getStore()->getId())
->where('is_system=?', 1)
- ->where('category_id=? OR category_id is NULL', $this->_urlRewriteCategory)
+// excluding this clause to facilitate one URL per product, and one that includes the category
+// if a product has multiple categories, the first one (by category_id) will be used
+// in most cases you'll probably only have one category because you only want one page per product for SEO reasons
+// for maximum link juice, no possibility of duplicate content, and a less confusing store
+// ->where('category_id=? OR category_id is NULL', $this->_urlRewriteCategory)
->where('product_id IN(?)', $productIds)
->order('category_id DESC'); // more priority is data with category id
$urlRewrites = array();

In my setup, each product only has one category. Although this will also work if you specify multiple categories per product, you won’t have complete control over which category is used for the official link.