Remove category path from product URL in magento

piaoling  2012-09-04 22:58:46

As great as Magento is there are a number of failings it can have from an search engine optimisation point of view and one of the biggest bugbears our SEO consultants have had with it is the number of different URLs a product can be accessed on.


If product a is in category 1 and two then by default magento will create links to it on the following URLs.


  • www.mystore.com/category-1/product-a.html
  • www.mystore.com/category-2/product-a.html
  • www.mystore.com/product-a.html


This can cause duplicate content issues from a search engine point of view. One solution is ensuring that you have canonical URLs in place so the search engine knows which is the primary URL. Better still is to ensure the same URL is produced for the product each and every time by removing the category part.


The file to edit is /app/code/core/Mage/Catalog/Model/Url.php though i advise rather than editing this directly you create a copy in /app/code/local/Mage/Catalog/Model/Url.php.


Then within function getProductRequestPath (around line 685) amend the following code

01./**
02.* Prepare product base request path
03.*/
04.if($category->getLevel() > 1) {
05.// To ensure, that category has path either from attribute or generated now
06.$this->_addCategoryUrlPath($category);
07.$categoryUrl= Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),
08.false,$storeId);
09.$requestPath=$categoryUrl.'/'.$urlKey;
10.}else{
11.$requestPath=$urlKey;
12.}

to this

01./**
02.* Prepare product base request path
03.*/
04./*if ($category->getLevel() > 1) {
05.// To ensure, that category has path either from attribute or generated now
06.$this->_addCategoryUrlPath($category);
07.$categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),
08.false, $storeId);
09.$requestPath = $categoryUrl . '/' . $urlKey;
10.} else {*/
11.$requestPath=$urlKey;
12.//}

And for good measure also amend the function generatePath to edit around line 831 and comment out this section.

01./*if ($category->getLevel() > 1) {
02.// To ensure, that category has url path either from attribute or generated now
03.$this->_addCategoryUrlPath($category);
04.$categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),
05.false, $category->getStoreId());
06.return $this->getUnusedPath($category->getStoreId(), $categoryUrl . '/' . $urlKey . $productUrlSuffix,
07.$this->generatePath('id', $product, $category)
08.);
09.}*/

and around 851

1./*if ($category && $category->getLevel() > 1) {
2.return 'catalog/product/view/id/' . $product->getId() . '/category/' . $category->getId();
3.}*/

and around 348

1./*if ($category->getLevel() > 1) {
2.$categoryId = $category->getId();
3.$updateKeys = false;
4.}*/

The next file to edit is /app/code/core/Mage/Catalog/Model/Product/Url.php (again I advise rather than editing this directly you create a copy in /app/code/local) in which we need to ensure the categoryid used for generating the links is always set to null. Edit the function getUrl around line 172 and make the follow amend


01./* commenting this out and setting the categoryId to null everytime
02.if (isset($params['_ignore_category'])) {
03.unset($params['_ignore_category']);
04.$categoryId = null;
05.} else {
06.$categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId()
07.? $product->getCategoryId() : null;
08.}*/
09. 
10.$categoryId= null;

Then hey presto now all the product links in your store should ignore the category path and cut down on canonicalisation issures. I am still in the early days of testing this thus far but have not experienced any problems from it yet.

If you are not comfortable in editing the code yourself then stayed tuned for our all singing all dancing SEO module for magento which will give you greater control over not just this but several other search engine optimisation features of magento.


need reindex

类别 :  默认(751)  |  浏览(9505)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: