How To setup URL Rewrites In Magento 2


SEO is a phenomenal online marketing strategy that enables Magento store owners to create fast, user-friendly, and robust websites that rank higher in search engines. Ranking high in search results brings more qualified customers to your online store and eventually leads to increased conversion rates.  For this reason, setting up the URL rewrites on your Magento store is momentous to the success of your online marketing strategy.

URL rewrites provide an expedient and secure way of making the existing URLs SEO-friendly and easier to read by humans. Magento is engineered to routinely utilize URL rewrites to eliminate the file value “index.php” that features in existing URLs just after the root folder. When the Web Server Apache Rewrites are activated any word that adds no value to SEO is removed.

This tutorial will help you set up URL rewrites in Magento 2 to aid your website performance, improve search engine ranking, and enhance the readability of your text.

Ready? Let’s get started!

Step 1:
Enabling URL Rewrites

To enable URL rewrites in Magento 2, log in to your store’s back-end. Click Stores, select Configuration and expand the Web section.

How To setup URL Rewrites In Magento 2

Click the Arrow on the Search Engine Optimization to open the drop-down menu. Locate the Use Web Server Rewrites option.

How To setup URL Rewrites In Magento 2

Set this option to YES.

How To setup URL Rewrites In Magento 2

Next, click Save Config for the changes to take effect.

How To setup URL Rewrites In Magento 2

Step 2:
Setting Up Automatic URL Redirects

Configuring automatic URL redirects allows your Magento 2 store to automatically initiate permanent redirect every time the URL key of any product is changed. To successfully setup automatic URL redirects in Magento 2 do the following.

Login to your Magento 2 Admin Area. Click Stores, select Configuration and expand the Catalog section.

How To setup URL Rewrites In Magento 2

From the drop-down options, select Search Engine Optimization and set Create Permanent Redirect for URL if URL Key is Changed to YES.

How To setup URL Rewrites In Magento 2

Click Save Config to implement the changes.

How To setup URL Rewrites In Magento 2

Step 3:
Using Codes To Manage URL Rewrites in Magento 2.

Another effective way of managing URL redirects is activating the 301 redirect. To accomplish this, follow the steps below:

  • Generating the constructor file using the command below:

* @var MagentoUrlRewriteModelResourceModelUrlRewriteFactory
protected $_urlRewriteFactory;
* @param Context $context
* @param MagentoUrlRewriteModelResourceModelUrlRewriteFactory $urlRewriteFactory
    Context $context,
    MagentoUrlRewriteModelResourceModelUrlRewriteFactory $urlRewriteFactory
) {
    $this->_eavAttributeFactory = $eavAttributeFactory;
  • Creating A Custom URL Rewrite In The Execute Method

If your actual URL is, then you can create URL redirect on a requested URL such as This can be achieved using the method below:

$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 */
$urlRewriteModel->setIdPath(rand(1, 100000));
/* set actual url path to target path field */
/* set requested path which you want to create */
/* set current store id */

Congratulations! You have successfully created custom URL programmatically.


That is it! You have successfully configured URL redirects in your Magento 2 store. By setting up the URL redirects, it becomes easy to redirect your store visitors from the old catalog pages to the newly created products. Besides, this will boost your visibility online by enabling you to use high-value keywords that offer unprecedented support for product index in search engine optimization.


Check out the top 3 Magento hosting services:

Was this article helpful?