How to add URL Rewrites programmatically in Magento 2?

In this blog, we discuss URL Rewrites programmatically in Magento 2, it is the best solution for online vendors like you who desire to truly build a huge number of the traffics of your websites. The function of rewriting the URL is to allow you to produce 301 redirects into Magento 2 programmatically.

Magento is one such platform that helps the users in all ways right from building an online store, setting up the site, configuring with new techs and scaling with numerous customization from admin panel.

Looking to turn your Magento store in to a Multi-vendor marketplace? check here
          Get your free live demo now: Click here

How to add URL Rewrites programmatically in Magento 2?

Image result for magento 2

URL redirects

A URL redirect is a major term in SEO, and it is used for navigating the visitors to any link store owners expect. There are two main types of redirect

  • 301 redirect
  • 302 redirect

Therefore, if you are wondering how to maintain working with the existing visitor at the current site while you are planning to make a new site with more efficiencies, creating the search redirection through redirect 301 is the best answer.

The below-mentioned steps help you to add URL Rewrites programmatically in Magento 2:

Step 1: Create a constructor file

Step 2: Construct custom URL rewrites in execute method

Create constructor file


* @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory


protected $_urlRewriteFactory;


* @param Context $context

* @param \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory


ublic function __construct(

    Context $context,

    \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory

) {

    $this->_eavAttributeFactory = $eavAttributeFactory;





Construct custom URL rewrites in execute method In Magento 2

The definite website URL is, but you want to execute the URL as URL) then you can build by the following method:

$urlRewriteModel = $this->_urlRewriteFactory->create ()

/* set current store id */


/* the following url is not formed by system so set as 0 */


/* unique identifier - place random unique value to id path */

$urlRewriteModel->setIdPath(rand(1, 100000));

/* place actual url path to target path field */


/* set requested path which you desire to form */


/* set current store id */


So yes, I hope the above piece would have helped in a better way, mostly for the Magento users who address their need for a sure assistant to work on the URL of Magento CMS pages programmatically.

We made it as simple as possible to implement in the right way for a brand-new page of the Magento store. Please do share with all developers and store owners who look to rewrite their page URLs because the methods mentioned here work for everyone.

Do contact us for further assistance on Magento development related queries, thanks.

Rewrites URLs in Magento 2

Last Update: March 1, 2022  

February 20, 2018   18816   Nandini Ramachandran    Magento, Magento 2 Tutorial    
Total 6 Votes:

Tell us how can we improve this post?

+ = Verify Human or Spambot ?


Leave a Reply

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