×
Login or create an account
If you have an account with us, please log in.

You have no items in your shopping cart.

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 URL Rewrites via the Admin Panel?

  • Log in to the Admin Panel;
  • Navigate to Stores>Configuration>Web:

general-web-magento

  • Click the drop-down menu of the Search Engine Optimization section and set the Use Web Server Rewrites to ‘Yes’:

web-server-optimization-magento

Click the Save Config button.

How to configure automatic URL Redirect?

  • Navigate to Stores>Configuration>Catalog>Catalog:

stores-catalog-magento

  • Choose the Search Engine Optimization drop-down menu;
  • Set Create Permanent Redirect for URLs if URL Key Changed to ‘Yes’:

create-permanent-magento

Click the Save Config button.

How to change product/category page URLs?

  • Go to Products>Catalog, choose a needed product/products;
  • Extend the drop-down Search Engine Optimization menu:

url-key-magento

  • Update the 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:

rewrite-table-magento

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:

add-new-url-magento

  • 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, 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:

E.g.:

/**
* @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();

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.

Rate the answer?
Back to top