Facebook like button count inconsistancy due to automatic url rewriting
-
12-12-2019 - |
Question
I was trying to add the facebook like button to my product pages but then I noticed that the like counter has different numbers depending on where the page was accessed from and the URL it has.
Magento by default creates 3 URL rewrites for each new product automatically.
- If I access a product from my home page it has the URL
mysite/product-name.html
. - If I access a product from a base category it has the URL
mysite/base-category/product-name.html
- If I access a product from a sub-category it has the URL
mysite/base-category/sub-category/product-name.html
all of the above have different like counts and are treated as different pages. is there a way to merge the like counts while still keeping the SEO friendly URLs? what are the best practices in this regard?
Thank you.
Solution 3
In the end the canonical meta tags did not solve the issue like I initially thought they did.
I ended up solving this issue by creating a helper function that query the url rewrite collection for a product id and then retrieves the product url with the deepest category levels (the url with the most '/'). and then I used this helper to get the product url for the social buttons, and I used it for the item link in list.phtml and new.phtml.
so now all my links refer to a single rewrite.
I don't know if this is the best solution but it does the job for me at least for now.
Helper code:
public static function getFullProductUrl(
Mage_Catalog_Model_Product $product
) {
$url = Mage::getModel('core/url_rewrite')->getCollection()
->addFilter('is_system', '1')
->addFilter('product_id', $product->getId())
->addFilter('store_id', Mage::app()->getStore()->getId())
->addFieldToFilter('category_id', array('gt' => '0'));
$items = $url->getItems();
$count = 0;
foreach ($items as $item) {
$sc = substr_count($item->getRequestPath(),'/');
if ($sc > $count){
$count = $sc;
$which = $item;
}
}
if (!$which->getRequestPath()) {
return $product->getUrlPath();
}
return Mage::getBaseUrl() . $which->getRequestPath();
}
OTHER TIPS
I guess using canonical meta tags to link the pages with each other will solve the issue. To enable them, Login to admin, go to System > Configuration > Catalog
and then click on the option labelled "Search Engine Optimizations". Enable for products or categories accordingly.
You can also set product url to NOT include category path in admin > system > configuration > catalog > seo
area.