Magento 2 Add URL Rewrite programmatically
Programmatically creating URL rewrites in Magento 2 is an excellent solution for online retailers looking to increase website traffic. The main objective of URL rewriting is to enable the generation of 301 redirects in Magento 2 through a programmatic approach.
The following steps will guide you in easily adding URL rewrites programmatically in Magento 2. Before diving in, let’s first understand the concept of URL redirects.
What is URL Redirect?
URL redirect is a common term in SEO, used to guide visitors to any link that store owners desire. There are two primary types of redirects: the 301 redirect and the 302 redirect.
If you’re looking to maintain the experience of existing visitors on your current site while planning to build a more efficient new site, the best approach is to implement a search redirection using a 301 redirect.
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 ); }
Adding a URL rewrite programmatically in Magento 2 is a powerful way to manage your site's URLs and enhance SEO performance. This approach allows you to create custom URL paths that lead to specific controllers and actions within your store, improving user experience and accessibility.
To implement a URL rewrite, you will typically work with the URL Rewrite model provided by Magento. This model allows you to define the original URL path and the new requested path, enabling smooth redirections and maintaining the flow of traffic to your site.
Step 2: Insert Custom URL Rewrite in Execute Method
If your original website URL is yourdomain.com/customModule/customController/customAction and you want to rewrite it to www.yourdomain.com/test, you can achieve this by executing the following method:
$urlRewriteModel = $this->_urlRewriteFactory->create() $urlRewriteModel->setStoreId(1); $urlRewriteModel->setIsSystem(0); $urlRewriteModel->setIdPath(rand(1, 100000)); $urlRewriteModel->setTargetPath("www.yourdomain.com/customModule/customController/customAction"); $urlRewriteModel->setRequestPath("www.yourdomain.com/test"); $urlRewriteModel->save();
One of the primary benefits of programmatically adding URL rewrites is the flexibility it offers. Whether you're migrating to a new URL structure or simply want to enhance existing links, this method ensures that you can do so efficiently without manual intervention.
Furthermore, using a programmatic approach helps maintain consistency across your store, especially if you're managing a large catalog with numerous products or categories. By automating the process, you reduce the risk of errors that can occur with manual updates.
Overall, programmatically adding URL rewrites in Magento 2 is an essential practice for developers looking to optimize their eCommerce sites. It not only supports better SEO strategies but also contributes to a seamless shopping experience for customers.
Please complete your information below to login.