In this blog, we discuss URL Rewrites programmatically in Magento 2, it is the best solution for online vendors like you who desire to truly build a huge number of the traffics of your websites. The function of rewriting the URL is to allow you to produce 301 redirects into Magento 2 programmatically.
Magento is one such platform that helps the users in all ways right from building an online store, setting up the site, configuring with new techs and scaling with numerous customization from admin panel.
Looking to turn your Magento store in to a Multi-vendor marketplace? check here
Get your free live demo now: Click here
How to add URL Rewrites programmatically in Magento 2?
URL redirects
A URL redirect is a major term in SEO, and it is used for navigating the visitors to any link store owners expect. There are two main types of redirect
- 301 redirect
- 302 redirect
Therefore, if you are wondering how to maintain working with the existing visitor at the current site while you are planning to make a new site with more efficiencies, creating the search redirection through redirect 301 is the best answer.
The below-mentioned steps help you to add URL Rewrites programmatically in Magento 2:
Step 1: Create a constructor file
Step 2: Construct custom URL rewrites in execute method
Create constructor file
/** * @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory */ protected $_urlRewriteFactory; /** * @param Context $context * @param \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory */ ublic function __construct( Context $context, \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory ) { $this->_eavAttributeFactory = $eavAttributeFactory; parent::__construct( $context ); }
Construct custom URL rewrites in execute method In Magento 2
The definite website URL is www.example.com/customModule/customController/customAction, but you want to execute the URL as www.example.com/xyz(requested URL) then you can build by the following method:
$urlRewriteModel = $this->_urlRewriteFactory->create () /* set current store id */ $urlRewriteModel->setStoreId(1); /* the following url is not formed by system so set as 0 */ $urlRewriteModel->setIsSystem(0); /* unique identifier - place random unique value to id path */ $urlRewriteModel->setIdPath(rand(1, 100000)); /* place actual url path to target path field */ $urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction"); /* set requested path which you desire to form */ $urlRewriteModel->setRequestPath("www.example.com/xyz"); /* set current store id */ $urlRewriteModel->save();
So yes, I hope the above piece would have helped in a better way, mostly for the Magento users who address their need for a sure assistant to work on the URL of Magento CMS pages programmatically.
We made it as simple as possible to implement in the right way for a brand-new page of the Magento store. Please do share with all developers and store owners who look to rewrite their page URLs because the methods mentioned here work for everyone.
Do contact us for further assistance on Magento development related queries, thanks.
Last Update: March 1, 2022
Very useful blog. Thanks for sharing.
we are glad to know such technical blog about Magento. Very informative post helped me lot.
I have got major issues on this subject. Your blog helped me to fix my issues. Thank you
Thanks for your comment henry
Hi Nandini
Where we need to put step2 code?
You can use step 2 code in execution method
public function execute()
{
}
Its works Thanks Magento developer for sharing this blog
Its works perfectly. Much Appreciated for your efforts !! Thanks for the wonderful article.
Amazing Blog Post. I have also read another related blog for changing the magento 2 product or category age URL and to check magento 2 URL rewrite record. Hope it will add more valuable knowledege for the rest of the readers.
https://www.fmeextensions.com/blog/configure-magento-2-url-rewrites/
thanks for sharing new trends in artificial intelligence and video streaming.
useful blog