---
title: "How to Add URL Rewrite for Product Programmatically using Root Script in Magento 2?"
url: "https://magecomp.com/blog/add-url-rewrite-for-product-programmatically-using-root-script-magento-2/"
date: "2023-05-26T13:26:05+00:00"
modified: "2025-02-13T12:33:00+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 371
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "In Magento 2, you can add product URL rewrite to create user-friendly and SEO-friendly URLs. Learn how to accomplish it using root script"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Programmatically Reorder in Magento"
    url: "https://magecomp.com/blog/programmatically-reorder-magento/"
  - title: "7 Things to Know before you Create Magento 2 Migration Plan"
    url: "https://magecomp.com/blog/7-things-to-know-before-you-create-magento-2-migration-plan/"
  - title: "How to Install Magento SUPEE 10266 With or Without SSH"
    url: "https://magecomp.com/blog/install-magento-supee-10266-with-or-without-ssh/"
---

# How to Add URL Rewrite for Product Programmatically using Root Script in Magento 2?

_Published: May 26, 2023_  
_Author: Dhiren Vasoya_  

![How to Add URL Rewrite for Product Programmatically using Root Script in Magento 2](https://magecomp.com/blog/wp-content/uploads/2023/05/How-to-Add-URL-Rewrite-for-Product-Programmatically-using-Root-Script-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

In this Magento 2 blog illustration, I will explain How to Add URL Rewrite for Product Programmatically using Root Script in Magento 2.
URL rewrite involves modifying the URL of website pages to create more user-friendly URLs. In Magento 2, you can [**Add URL Rewrite from Admin**](https://magecomp.com/blog/magento-2-url-rewrite/) or [**Add URL Rewrite Programmatically**](https://magecomp.com/blog/add-url-rewrite-programmatically-magento-2/).
[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-5.webp)](https://magecomp.com/services/hire-magento-programmer/)

By default, Magento generates the URL key for a product based on its name or specified attribute values. However, you can modify or override the URL key to create a custom URL for a product using the URL rewrite functionality.
Product URL rewrite allows you to customize the URL of the product page of your Magento 2 store. It enables you to create SEO-friendly URLs, manage URL redirects, and improve the overall user experience by providing more descriptive and memorable URLs for your products. They play a crucial role in SEO optimization and ensuring that customers can access product pages through clean and meaningful URLs.
## Steps to Add URL Rewrite for Product Programmatically using Root Script in Magento 2:
**Step 1:** Create **Producturl.php** in your Magento root directory and add the following code.
```
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';

try{
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $state = $objectManager->get('MagentoFrameworkAppState');
    $state->setAreaCode('frontend');

    $productIds = ['1','2']; // Product Ids

    foreach($productIds as $productId){
        $urls = [];
        $product = $objectManager->create(MagentoCatalogModelProduct::class)->load($productId);
        $urls[] = $objectManager->create(MagentoCatalogUrlRewriteModelProductUrlRewriteGenerator::class)->generate($product);

        $urlPersist = $objectManager->create(MagentoUrlRewriteModelUrlPersistInterface::class);
        $urlPersist->replace(array_merge([], ...$urls));

        $product->setStoreId(1);
        $product->setWebsiteIds(['1']);
        $product->save();
    }
    echo "Product URL generated successfully";
}
catch(Exception $e)
{
    print_r($e->getMessage());
}

?>
```

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

Product URL will be generated if not available in the current URL rewrite table.
[![Product URL Rewrite](https://magecomp.com/blog/wp-content/uploads/2023/05/Product-URL-Rewrite.png)](https://magecomp.com/blog/wp-content/uploads/2023/05/Product-URL-Rewrite.png)

## Conclusion:
This way, you can add product URL rewrite programmatically using root script in Magento 2. If you face any issues, share them with me through the comment box without any hesitation. I will be quick to provide you with the solution. Moreover, share the tutorial with your friends to help them add product URL rewrites in their Magento 2 stores.
[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-5.webp)](https://magecomp.com/magento-malware-removal.html)
**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/add-url-rewrite-for-product-programmatically-using-root-script-magento-2/](https://magecomp.com/blog/add-url-rewrite-for-product-programmatically-using-root-script-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-03 13:26:06 UTC_  
