How to Manage Magento 2 URL Rewrite
A Magento 2 store may undergo changes from time to time. There may be changes in product or catalogue pages or changed the location entirely! In such cases, if the URL rewriting is not taken care of, then the store might lose potential customers due to landing on a page that does not exist anymore. Also, the admin would want to rewrite the URL with higher-value keywords to make it more search friendly and improve the SEO of the store. If you’re going to do the same for your store, learn how to manage the Magento 2 URL rewrite! Remember, a misconfigured URL rewrite can cause Magento 2 404 error.
Enable Magento 2 URL rewrite for your store when you want to redirect the old links to a new address. There are two ways to enable URL rewrites in Magento 2
Herre, I have explained both the methods stepwise.
-
URL Rewrites via the Admin Panel
- Follow the below steps
- Configure automatic URL Redirect
- Change product or category page URLs
- Go to Products > Catalog, select the products of which the URLs has to be rewritten
- Select the drop-down menu of the Search Engine Optimization section
- Update the URL Key here, and use only lowercase characters
- Save the changes and refresh the cache.
Configuring these settings will give you the desired results. The visitors will be redirected to the new page from the old URL.
- See Magento 2 URL Rewrite tableNavigate to Marketing > SEO&Search > URL Rewrites in Admin Panel to see the redirect records. As shown in the image below, the most recent redirects will appear at the top of the Magento 2 URL Rewrites table.
- Add URL Rewrite using the Add URL Rewrite buttonGo to Marketing > SEO&Search > URL Rewrites. If you have followed the instructions above, then you are in the required tab. Click the “Add URL Rewrite” button. Configure the following settings:
- Create URL Rewrite: Choose the custom option for category, product or CMS page.
- Store: Select the store view
- Request Path: Input a new URL key and suffix for the category or the product
- Target Path: Enter the targetted path
- Redirect Type: Choose either Temporary (302) or Permanent (301) redirect type
- Description: Describe the rewrite
- Save your settings
Fulfill reindex via the backend or use the command: php bin/magento indexer:reindex in the case where the changes do not reflect in the result.
-
Magento 2 URL Rewrite Programmatically
Enable 301 URL redirect to create search redirection to go on working with the customers after building new URLs.
Follow the below steps:
- Generate a constructor file
1234567891011121314151617<?php/*** @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory*/protected $_urlRewriteFactory;/*** @param Context $context* @param \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory*/public function __construct(Context $context,\Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory){$this->_eavAttributeFactory = $eavAttributeFactory;parent::__construct($context);} - Fill in custom URL rewrite in execute method
12345678910111213$urlRewriteModel = $this->_urlRewriteFactory->create();/* set current store id */$urlRewriteModel->setStoreId(1);/* this url is not created by system so set as 0 */$urlRewriteModel->setIsSystem(0);/* unique identifier - set random unique value to id path */$urlRewriteModel->setIdPath(rand(1, 100000));/* set actual url path to target path field */$urlRewriteModel->setTargetPath("www.example.com/customModule/customController/customAction");/* set requested path which you want to create */$urlRewriteModel->setRequestPath("www.example.com/xyz");/* save URL rewrite rule */$urlRewriteModel->save();
- Generate a constructor file
That’s it.
Now besides the importance of URL rewrite you know how to create and manage it.
You may also love reading our another blog post on how to programmatically set URL key of category pages in Magento 2.
Any doubts regarding URL rewrite will be entertained in the comment section.
Don’t forget to flash 5 stars if you like my blog.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
6 Comments
Dear Jethva, I have a question about installation of Magento 2.3.4.
I try to upgrade the current website “www.kasteelwijnen.com” to magento 2.3.4. “www.m2.kasteelwijnen.com”
Using the magento guide I have changed the admin url adding home in the 2 url.
Now it is for me not possible any more to login tin he backoffice 1
How can I solve this problem?
1) 1https://m2.kasteelwijnen.com/admin_18fqfa2/admin/index/index/key/12ec00bd5e41a46bfe6a102855416d2c16ccac236f675035e0a8434e0cf9e72a/
Hello Van der Pool,
Have you changed the admin URL using backend custom admin URL setting?
https://m2.kasteelwijnen.com/admin_18fqfa2/admin this URL is accessible!
Thank You
Thanks for sharing very helpful information for URL Rewriting…
Hello Aalee,
We’re glad that our blog is helpful for you.
Thank You
Am running a store in Magento2 which I recently migrated from another platform. So all my existing URL of products changed but keeping the main domain as same.
It is a tedious task to rewrite URL for all the 550+ products. Is there any workaround to redirect all the product domains at once?
Hello,
You can find the solution in a database table named url_rewrite
Thank you.