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.
Click the Save Config button.
To regenerate URL rewrites automatically in Magento 2 follow these steps.
Click the Save Config button.
As soon as we have configured URL Redirects in previous steps, the old URL will redirect users to a new one.
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.
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.
E.g. Magento redirect URL with parameters:
/**
* @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:
$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();
Automate SEO-friendly URL rewrites regeneration with URL Rewrites Regenerator →
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.
If URL rewrites aren't working in Magento 2, it could be due to several factors, ranging from configuration issues to caching or database problems. Below are the options that you can to try to fix the issue.
1. Caching issues can prevent new rewrites from taking effect. To fix this, you will need to clear Magento Cache. Run the following commands to clear cache:
php bin/magento cache:clean
php bin/magento cache:flush
2. Check URL rewrite table in the database. The URL rewrites are stored in the url_rewrite
table in the database. You can manually inspect the table to ensure that the rewrite rules exist and are correct. Here's a SQL that you can use:
SELECT * FROM url_rewrite WHERE request_path = 'your-path';
If there are any missing or incorrect entries, you may need to reinsert or update them.
3. Reindexing Magento can help resolve issues with URL rewrites if indexing is outdated:
php bin/magento indexer:reindex
Magento redirect is not working? Check how to fix the issue →
Login and Registration Form