Back to Top

Magento2 Programmatically Create Custom URL Rewrites

Updated 4 March 2024

URL Rewrites

Edit on GitHubLog an Issue

The URL Rewrite tool lets you change any URL which is in association with any product, category, or CMS page. When the rewrite comes into effect, any links that point to the previous URL will redirect to the new address.

To quickly update URL rewrites for multiple or all products simultaneously, refer to Multiple URL Rewrites.

The terms rewrite and redirect often used interchangeably, but refer to slightly different processes. URL rewrite changes the way a URL appears in the browser. A URL redirect update the URL which is store on to the server. A URL redirect can be either temporary or permanent. Your store uses URL rewrites and redirects to make it easy for you to change the URL key of a product, category, or page and preserve existing links.

Magento2 Programmatically Create Custom URL Rewrites  :

Searching for an experienced
Magento 2 Company ?
Find out More

For normal users there is a common need of custom URLs for controlling their websites and they can create their custom URLs using magento2 Admin panel user interface from Marketing -> SEO & Search -> Url Rewrites

To manage URL rewrite features with custom module :   If you want to create custom URLs programmatically in Magento 2 then the solution is already here in this blog. Here I am going to explain the complete process of how to create custom URL using code in controller file.

Step 1 : Create constructor file.

* @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;

Step 2 : Create custom URL rewrite in execute method.

Like your website actual URL is “customModule/customController/customAction” but you want to execute this URL on click on “mycontrollerpath” (requested URL) then you can create by following method-

$urlRewriteModel = $this->_urlRewriteFactory->create();
/* set current store id */
/* this url is not created by system so set as 0 */
/* unique identifier - set random unique value to id path */
$urlRewriteModel->setIdPath(rand(1, 100000));
/* set actual url path to target path field */
/* set requested path which you want to create */
/* set current store id */

Magento2 Programmatically Create Custom URL Rewrites

So in this way you can create custom Rewrite URLs in Magento 2. Thanks!

You can also check :

. . .

Leave a Comment

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


  • E.J. Sexton
    • ashutosh srivastava (Moderator)
  • SAndeep
  • Rafael Corrêa Gomes ♛
  • satish.patel438
  • zarana Patel
    • satish.patel438
      • SAndeep
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home