If you’ve been running an eCommerce store for a while, you already know how important clean and SEO-friendly URLs are.
Both customers and search engines love simple, meaningful URLs instead of long, confusing ones filled with IDs or parameters.
For example:
- ❌ https://www.example.com/catalog_id/?id=3
- ✔️ https://www.example.com/products/dash-digital-watch
See the difference?
The second one is not only easier for customers to remember but also improves your site’s SEO performance.
That’s where URL rewrites in Magento 2 come into play.
In this guide, we’ll walk you through how to rewrite URLs programmatically in Magento 2 so you can redirect customers to the right product, category, or CMS page without breaking the user experience.
Why Rewrite/Redirect URLs in Magento 2?
- SEO Benefits: Search engines prefer URLs that are short, descriptive, and meaningful. And if other websites are linking to your old URLs, rewriting them ensures you don’t lose that SEO value. A proper 301 redirect passes on most of the ranking power to the new URL, protecting your site’s authority.
- Better User Experience: Customers trust websites that look professional, and a clean URL contributes to that trust. A readable link tells users exactly what to expect before they click, reducing confusion and improving overall navigation.
- Redirection Control: When products go out of stock, categories change, or CMS pages are updated, old URLs can easily break. By setting up URL redirects in Magento 2, you can redirect users from outdated or broken links to relevant new pages—ensuring you don’t lose valuable traffic.
Creating a URL Rewrite Programmatically in Magento 2
Magento 2 gives us a handy class, Magento\UrlRewrite\Model\UrlRewriteFactory, that helps create custom URL rewrites.
Let’s see how to implement it inside a controller.
<?php
namespace Vendor\Module\Controller;
use Magento\Framework\App\Action\Context;
class YourController
{
/**
* @var \Magento\UrlRewrite\Model\UrlRewriteFactory
*/
protected $_urlRewriteFactory;
/**
* @param Context $context
* @param \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
*/
public function __construct(
Context $context,
\Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
) {
$this->_urlRewriteFactory = $urlRewriteFactory;
parent::__construct($context);
}
public function execute() {
$urlRewriteModel = $this->_urlRewriteFactory->create();
/* Define what type of entity you are rewriting (custom, cms-page, product, category) */
$urlRewriteModel->setEntityType('custom');
/* Store ID (use the store you want this rewrite for) */
$urlRewriteModel->setStoreId(1);
/*set 0 as this is a custom rewrite (not system generated) */
$urlRewriteModel->setIsSystem(0);
/* Unique identifier for this rewrite - place random unique value to ID path */
$urlRewriteModel->setIdPath(rand(1, 100000));
/* Actual path where the request should land */
$urlRewriteModel->setTargetPath(‘target-url.html’);
/* Set requested path which you want to create */
$urlRewriteModel->setRequestPath(‘requested-url.html’);
/* The type of Redirect (301 = permanent, 302 = temporary) */
$urlRewriteModel->setRedirectType(301);
/* Save the URL rewrite rule */
$urlRewriteModel->save();
}
}
|
Explanation of the Code
- Entity Type→ Defines if the rewrite is for a CMS page, category, product, or a custom URL.
- Store ID→ Make sure to set the correct store view where this rewrite applies.
- isSystem(0)→ Ensures it’s not a system-generated rewrite but a custom one.
- IdPath→ Needs to be unique, so using rand() works fine.
- Target Path→ The actual internal path you want to redirect to.
- Request Path→ The new, SEO-friendly URL customers will see.
- Redirect Type→ 301 for permanent, 302 for temporary.
This gives you full control over how your customers reach your content while maintaining a clean URL structure.
Conclusion
We hope this blog helps you to clearly understand how to create url rewrite programmatically in Magento 2.
This simple customization can go a long way in improving SEO, user experience, and overall site navigation.
If you run into any issues while implementing this, don’t hesitate to reach out to our Magento experts. We’re always happy to help.