Prefixing Magento Category and Product URLs
In Magento, by default, category and product URLs follow this structure:
www.example.com/category-slug/
www.example.com/category-slug/product-slug/
However, there are cases where you might want to prefix these URLs with something like “products” or “categories”. For example:
www.example.com/products/category-slug/
www.example.com/products/category-slug/product-slug/
This is especially useful when integrating Magento with a third-party CMS to avoid URL collisions. In this guide, I’ll show you how to achieve this while keeping category URL rewrites enabled for SEO purposes.
Understanding the Default Behavior
Before we start, let’s understand why Magento URLs are structured this way:
Magento uses a flat taxonomy structure for categories. Each category has its own URL key (slug), which is used to generate the URL path. When you enable catalog URL rewrites, Magento creates clean, search engine friendly URLs.
Prefixing Category and Product URLs
To prefix your category and product URLs, we need to make changes in two places:
- The core
app/code/core/Mage/Catalog/Model/Url.phpfile. - The
_getProductUrl()method in the same file.
Here’s a step-by-step guide:
Step 1: Modify app/code/core/Mage/Catalog/Model/Url.php
Open this file and find the following function:
public function getCategoryRoute($category)
{
return 'catalog/category/view';
}
Change it to:
public function getCategoryRoute($category)
{
if ($category->getLevel() == 1) {
return 'products/catalog/category/view';
} else {
return 'catalog/category/view';
}
}
This will prefix the category URLs with “products” only for the root categories.
Step 2: Modify the _getProductUrl() Method
In the same file, find and modify the getCategoryRoute() method:
protected function getProductRoute($product)
{
return 'catalog/product/view';
}
Change it to:
protected function getProductRoute($product)
{
$category = Mage::getModel('catalog/category')->load($product->getCategoryIds());
if ($category && $category->getLevel() == 1) {
return 'products/catalog/product/view';
} else {
return 'catalog/product/view';
}
}
This will prefix the product URLs with “products” only when the product belongs to a root category.
Conclusion
By following this guide, you can successfully prefix your Magento category and product URLs while keeping category URL rewrites enabled. This is particularly useful when integrating Magento with other CMS systems to avoid URL path collisions.
Remember that making direct changes to core files is generally discouraged as it can cause issues during updates or overwrites of the core code. If possible, consider using a custom theme or module for these modifications.
For more advanced techniques in Magento development and integration, I recommend exploring resources like Laravel Company. They provide comprehensive guides on Magento development best practices and advanced features.
In conclusion, prefixing URLs in Magento requires careful modification of the core Url.php file. By following this guide, you can achieve your desired URL structure while maintaining SEO-friendly category URLs.