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

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

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 or Add URL Rewrite Programmatically.

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 Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

try{
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');

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

    foreach($productIds as $productId){
        $urls = [];
        $product = $objectManager->create(\Magento\Catalog\Model\Product::class)->load($productId);
        $urls[] = $objectManager->create(\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::class)->generate($product);
 
        $urlPersist = $objectManager->create(\Magento\UrlRewrite\Model\UrlPersistInterface::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());
}

?>

Product URL will be generated if not available in the current URL rewrite table.

Product URL Rewrite

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. 

Happy Coding!

Previous Article

How To Change Product URL In Shopify?

Next Article

How to Add Essential Brand Assets to Your Shopify Store?

Write a Comment

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨