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.

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

Leave a Reply

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