Magento 2 URL Rewrite
Let's assume that you need to redirect customers from the old product/catalog pages to newly-created ones. In this case, you may lose your clients and traffic if the old addresses are not redirected to the new ones. Thus, when you need any links that point to the previous URLs to be redirected to the new addresses, you should enable URL Rewrite for your Magento 2 store.
How to enable Magento 2 URL Rewrites via the Admin Panel?
- Log in to the Admin Panel;
- Navigate to Stores>Configuration>Web:
- Click the drop-down menu of the Search Engine Optimization section and set the Use Web Server Rewrites to ‘Yes’:
Click the Save Config button.
How to configure automatic URL Redirect?
To regenerate URL rewrites automatically in Magento 2 follow these steps.
- Navigate to Stores>Configuration>Catalog>Catalog:
- Choose the Search Engine Optimization drop-down menu;
- Set Create Permanent Redirect for URLs if URL Key Changed to ‘Yes’:
Click the Save Config button.
How to change Magento product/category page URLs?
- Go to Products>Catalog, choose a needed product/products;
- Extend the drop-down Search Engine Optimization menu:
- Update the magento URL Key here, don’t forget to use only lowercase characters;
- Save the changes and refresh the cache.
As soon as we have configured URL Redirects in previous steps, the old URL will redirect users to a new one.
How to see Magento 2 URL Rewrites table?
- To see the redirect records in the Admin Panel go to Marketing>SEO&Search>URL Rewrites. The most recent redirects will appear at the top of the Magento 2 URL Rewrites table:
How to add URL Rewrite using the same-named button?
- Navigate to Marketing>SEO&Search>URL Rewrites or stay on the same tab when followed the instructions described above. Click the Add URL Rewrite button:
- Complete the settings:
Create URL Rewrite: choose the option you need here (Custom, For category, For product, For CMS page);
Store: choose a required store view;
Request Path: fill in a new URL key and suffix for the product;
Target Path: insert the targeted path;
Redirect Type: choose either Temporary (302) or Permanent (301) redirect type;
Description: write a description for the rewrite if it’s needed.
- Click Save;
- In case the changes haven’t been applied and Magento 2 didn't rewrite the URL, we recommend to fulfill reindex via the backend or using the command: php bin/magento indexer:reindex
How to make Magento 2 URL Rewrite programmatically?
The most appropriate way to create search redirection to go on working with the customers after building new URLs is to enable the 301 redirect.
- Generate a constructor file:
* @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory
* @param Context $context
* @param \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteFactory $urlRewriteFactory
public function __construct(
$this->_eavAttributeFactory = $eavAttributeFactory;
- Fill in custom URL rewrite in execute method:
$urlRewriteModel = $this->_urlRewriteFactory->create()
/* set current store id */
/* this url is not created by system so set as 0 */
/* unique identifier - set random unique value to id path */
/* set actual url path to target path field */
/* set requested path which you want to create */
/* save URL rewrite rule */
Magento 2 URL Rewrite module
Besides, you can use the Unique Product URL extension. The module allows for creation of short and user-friendly product URLs as well as automatic redirecting secondary URLs to the main one.
With this extension installed, you won't have such problems as not working Magento 2 product URL rewrite and won't have to rebuild or delete URL rewrites.