Customizing Magento URL Rewrite for Products and Categories
As an e-commerce store owner or developer, you often want more control over your URLs to improve SEO and user experience. In this post, we’ll guide you through customizing the rewrite rules for both categories and products in Magento.
Understanding the Default Magento URL Structure
By default, Magento creates URLs that reflect the category hierarchy. For example:
- Category 1 > Subcat 11 > Subsubcat 11
- www.store.com/category-1/subcat-11/subsubcat-11/
And for products:
Setting Up Custom URL Rewrite Rules
To achieve the desired URL structure, you’ll need to create custom rewrite rules. Here’s a step-by-step guide:
Step 1: Create a Category URL Prefix
First, let’s focus on the category URLs.
The XML Approach
You can use an XML file in your theme directory (app/design/frontend-theme/Magento_Catalog/layout/default.xml) to define a new rewrite rule for categories. Here’s how:
<reference name="category.products.list">
<action method="setCategoryUrlSuffix">
<argument name="suffix" xsi:type="string">/</argument>
</action>
</reference>
This code tells Magento to set the category URL suffix as a slash (/), which removes the subcategories from the URL.
The PHP Approach
Alternatively, you can override the controller action class using a module. First, create an event observer (app/code/Vendor/Module/etc/events.xml):
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch_catalog_category_view">
<observer name="custom_category_urls" instance="Vendor\Module\Model\RewriteCategoryUrlObserver" />
</event>
</config>
Then, create the observer class (app/code/Vendor/Module/Model/RewriteCategoryUrlObserver.php):
class RewriteCategoryUrlObserver implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$controller = $observer->getControllerAction();
$categoryId = $controller->getRequest()->getParam('id');
if ($categoryId) {
$category = $this->_categoryFactory->create()->load($categoryId);
$controller->getResponse()->setHeader(
'Location',
$this->_urlBuilder->getUrl('catalog/category/view', ['_query' => ['id' => $category->getId()]])
);
}
}
}
This observer changes the URL of a category page to use the new format (e.g., www.store.com/categories/subsubcat-11/).
Step 2: Customizing Product URLs
For product URLs, you’ll need to modify the rewrite rules in the database directly or create a custom module.
Using Magento Database Directly
You can manually update the core_url_rewrite table and add new records with the desired URLs. However, this method is not recommended for production sites due to potential data inconsistencies.
Creating a Custom Rewrite Module
To maintain data consistency, it’s better to create a custom module that generates the rewrite rules automatically. Here’s a brief outline:
- Create your module and enable it.
- Add an observer to catch product save events. Update the
core_url_rewritetable with the new URL for each saved product. - Flush the URL rewrite cache when products are saved or deleted.
Remember, creating a custom module requires development skills in PHP and familiarity with Magento’s event system and database schema.
Conclusion
Customizing URLs in Magento can greatly improve your SEO strategy and user experience. By following this guide, you can create clean, intuitive URLs for both categories and products without subcategories in the URL path.
If you need more advanced customizations or want to learn how to implement these changes efficiently, consider checking out Laravel Company’s Magento development services. Our team of certified experts can help you optimize your store’s performance and functionality.
Don’t forget to backup your database before making any changes, especially when working with the database directly. Always test these modifications in a staging environment first to avoid potential issues on your live site.
Happy coding!