How To

How to Add URL Rewrite Programmatically in Magento 2

Hello Magento Friends,

In today’s Magento 2 tutorial, I am going to figure out How to Add URL Rewrite Programmatically in Magento 2.

You may require customers to redirect from the old URL to a new address. If the old address is not redirected to the new address, you may lose website traffic. Enabling URL rewrite for Magento 2 helps to redirect customers to new URL addresses easily. URL rewrite is used to make existing URLs more search-friendly.

Check How to Configure and Manage URL Rewrite in Magento 2.

Here we will learn about Adding URL Rewrite Programmatically in Magento 2.

Let’s check the steps

Steps to Add URL Rewrite Programmatically in Magento 2:

Step 1: To generate the constructor, you have to use the below block of code in your controller file. Here, we have used the Magento\UrlRewrite\Model\UrlRewriteFactory instance and create its object($urlRewriteFactory).

<?php
namespace Vendor\Extension\Controller;
use Magento\Framework\App\Action\Context;
class CustomController
{
     protected $urlRewriteFactory;
     public function __construct(
            Context $context,
            \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory)
     {
         $this->urlRewriteFactory = $urlRewriteFactory;
         parent::__construct($context);
     }
     public function execute() 
     { 
       $urlRewrite = $this->urlRewriteFactory->create();

       /*if you want to rewrite url for “custom” set entity type*/       $urlRewrite->setEntityType('custom');

       /*set current store ID */       $urlRewrite->setStoreId(1);

       /*set 0 as this url is not created by system */       $urlRewrite->setIsSystem(0);

       /* unique identifier - place random unique value to ID path */       $urlRewrite->setIdPath(rand(1, 100000));

       /* set actual url path to target path field */       $urlRewrite->setTargetPath("https://yourdomainname/customModule/customController/customAction");

       /* set requested path which you want to create */       $urlRewrite->setRequestPath("https://yourdomainname/abc");

       /* set the type of Redirect */       $urlRewrite->setRedirectType(301);

       /* save URL rewrite rule */       $urlRewrite->save();
    }
}

Conclusion:

Hence, this way you can Add URL Rewrite Programmatically in Magento 2. If you have any queries feel free to ask me. Share the article with other developers.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 3.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

18 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago