Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Programmatically Add URL Rewrite in Magento 2

Vinh Jacker | 03-17-2025

How to Programmatically Add URL Rewrite in Magento 2

Magento 2 URL Rewrite programmatically is one of the awesome solutions for online retailers who want to create a huge number of the traffics of your website. The purpose of rewriting the url is allowing you generating 301 redirects into Magento 2 by the programmatical way.

The below steps will help you add URL Rewrite programmatically in Magento 2 with ease.

Before that, let’s learn about URL Redirect.

What is URL Redirect?

URL redirect is a popular term in SEO and it is used for navigating the visitors to any link store owners expect. There are two main type of redirect: 301 redirect and 302 redirect. Therefore, if you are wondering how to continue working with the existing visitor at the current site while you are planning to build a new site with more efficiencies, the best answer is creating the search redirection through the redirect 301.

2 Steps to add URL programmatically in Magento

Step 1 : Generate 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;
    parent::__construct(
        $context
    );
}

Step 2 : Insert custom URL rewrite in execute method

Only if your original website is example. com/customModule/customController/customAction, you want to rewrite the URL by www.example. com/xyz, instead. Then you can run the following method:

$urlRewriteModel = $this->_urlRewriteFactory->create()
/* set current store id */
$urlRewriteModel->setStoreId(1);
/* this url is not created by system so set as 0 */
$urlRewriteModel->setIsSystem(0);
/* unique identifier - set random unique value to id path */
$urlRewriteModel->setIdPath(rand(1, 100000));
/* set actual url path to target path field */
$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");
/* set requested path which you want to create */
$urlRewriteModel->setRequestPath("www.example.com/xyz");
/* set current store id */
$urlRewriteModel->save();

Final words

I hope this tutorial helps you fix your issues regarding adding URL Rewrite programmatically in Magento 2. If you have any questions, feel free to let us know.

Related Post

x
    Jacker

    Jacker is the Chief Technology Officer (CTO) at Mageplaza, bringing over 10 years of experience in Magento, Shopify, and other eCommerce platforms. With deep technical expertise, he has led numerous successful projects, optimizing and scaling online stores for global brands. Beyond his work in eCommerce development, he is passionate about running and swimming.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services