How To: Add a prefix to Magento product URL’s

When disabling categories from product urls in Magento, you eliminate many search engine optimization issues. One issue, however, is that all of your product’s urls are in the top level of the websites structure.

While migrating a site from Volusion, we had the need to match Volusion’s URL structure for products, allowing a more seamless transition to Magento.

Volusion URL structure for products:
/product-p/blue-widget.htm

Default Magento URL Structure without categories:
/blue-widget.html

To match the URL’s to volusion, we made the following changes:

  1. Disable “Use Categories Path for Product URLs”
  2. “Product URL Suffix” from .html to .html
  3. Adding a prefix to magento’s product URL’s

Adding a Prefix to Product URL’s in Magento

This is a very simple modification that requires you duplicate and modify one file. Do not modify URL.php directly, when you update your changes will be overwritten.

Copy:
/app/code/core/mage/catalog/model/url.php

To:
/app/code/local/mage/catalog/model/url.php

At lines 779-787, Change:

 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;
        } 

To:

< 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-p/' .$categoryUrl . '/' . $urlKey;
        } else {
            $requestPath = 'product-p/' .$urlKey;
        }
} 

Above, find “product-p” and change it to your desired prefix for products.

This site is currently under development and the modification has not been fully tested. We have confirmed basic functionality.

This entry was posted in E-Commerce and tagged . Bookmark the permalink.

4 Responses to How To: Add a prefix to Magento product URL’s

  1. Ben Corlett says:

    This works great! Thank you very much. Was looking for a solution earlier this year as per recommendations from our SEO partners, however I couldn’t find anything.

    Thank you very much!

  2. Hi Dear

    This is not working in magento CE 1.9. Might be possible that they have changed the way magento loads the products of the file have been moved to somewhere else.
    If you have any idea how can i do above in magento 1.9 then please share how to add prefix in product urls in magento.

    • Rutherford says:

      I checked a 1.9 install and it did not change. There was a duplicate semi-colon in the code above for some reason that may have cause you an issue..

  3. Jaden says:

    Its worked for CE 1.9 after adding that code you have to run Catalog URL Rewrites from index management and clear cache as well.

Leave a Reply

Your email address will not be published. Required fields are marked *