Hello magento friends todays in this blog post im share with you to How to Add URL Rewrite for Product Programmatically using Root Script in Magento 2.
To add URL rewrite programmatically for a product in Magento 2 using a root script, you can follow these steps:
➤ Create a root script:
Create a PHP file in the root directory of your Magento 2 installation. For example, name it add_product_url_rewrite.php.
➤ Include Magento's bootstrap and initialization:
Add the following code at the beginning of the add_product_url_rewrite.php file to load the Magento 2 bootstrap and initialize the application:
➤ Replace PRODUCT_SKU, CATEGORY_ID, and NEW_URL_KEY with your desired values:
Modify the placeholders with the actual product SKU, the desired category ID where the product should appear, and the new URL key for the product.
➤ Create a function to add URL rewrite:
➤ Call the function:
After defining the function, call it with the appropriate parameters: