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;

    parent::__construct(

        $context

    );

}

Construct custom URL rewrites in execute method In Magento 2

The definite website URL is  www.example.com/customModule/customController/customAction, but you want to execute the URL as www.example.com/xyz(requested URL) then you can build by the following method:

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

/* set current store id */

$urlRewriteModel->setStoreId(1);

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

$urlRewriteModel->setIsSystem(0);

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

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

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

$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");

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

$urlRewriteModel->setRequestPath("www.example.com/xyz");

/* set current store id */

$urlRewriteModel->save();

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


Posted

in

,

by

Comments

11 responses to “How to add URL Rewrites programmatically in Magento 2?”

  1. MelvinTub Avatar
    MelvinTub

    Very useful blog. Thanks for sharing.

  2. Hoarrygon Avatar
    Hoarrygon

    we are glad to know such technical blog about Magento. Very informative post helped me lot.

  3. HenryPer Avatar
    HenryPer

    I have got major issues on this subject. Your blog helped me to fix my issues. Thank you

    1. Nandini R Avatar

      Thanks for your comment henry

  4. Joe Avatar
    Joe

    Hi Nandini
    Where we need to put step2 code?

    1. Nandini R Avatar

      You can use step 2 code in execution method
      public function execute()
      {
      }

  5. Elmerwax Avatar
    Elmerwax

    Its works Thanks Magento developer for sharing this blog

  6. Franky Avatar
    Franky

    Its works perfectly. Much Appreciated for your efforts !! Thanks for the wonderful article.

  7. David Jonathan Avatar

    Amazing Blog Post. I have also read another related blog for changing the magento 2 product or category age URL and to check magento 2 URL rewrite record. Hope it will add more valuable knowledege for the rest of the readers.

    https://www.fmeextensions.com/blog/configure-magento-2-url-rewrites/

  8. dhanasiri Avatar
    dhanasiri

    thanks for sharing new trends in artificial intelligence and video streaming.

  9. Raymond v Avatar
    Raymond v

    useful blog

Leave a Reply

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