Magento – Static product URL prefix

producturlurl-rewrite

I would like to add a static path to product URLs. Something like 'product/regular-product-path'. I would like this to be a rule, not something I have to add manually in Catalog >> URL Rewrite Manager.

I have found this post http://www.placementedge.com/blog/how-to-add-a-prefix-to-magento-product-urls/ where a solution is suggested. It apparently works, but it is probably targeted towards an earlier version of Magento because the script the writer suggests to edit doesn't exist in my 1.8.1 version.

He suggests editing /app/code/local/Mage/Catalog/Model/Url.php, but the close I could find was /app/code/local/Mage/Catalog/Model/Product/Url.php. If /app/code/local/Mage/Catalog/Model/Product/Url.php is the file I should be editing, which lines should I change to add my static URL prefix? Thank you.

Best Answer

You need to copy /app/code/core/mage/catalog/model/url.php file to /app/code/local/mage/catalog/model/url.php and at lines 779-787 replace the following code

if ($category->getLevel() > 1) {
   // To ensure, that category has path either from attribute or generated now
   $this->_addCategoryUrlPath($category);
   $categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),
    false, $storeId);
   $requestPath = $categoryUrl . '/' . $urlKey;
} else {
   $requestPath = $urlKey;
}

with

if ($category->getLevel() > 1) {
 // To ensure, that category has path either from attribute or generated now
 $this->_addCategoryUrlPath($category);
 $categoryUrl = Mage::helper('catalog/category')->getCategoryUrlPath($category->getUrlPath(),
  false, $storeId);
 $requestPath = 'product-prefix/' .$categoryUrl . '/' . $urlKey;;
} else {
 $requestPath = 'product-prefix/' .$urlKey;
}
Related Topic