Magento 2 URL Rewrite Full Tutorial You Should Not Miss

Sometimes, you may wonder if a friendly URL is really important to your website?  The answer is: absolutely! When it comes to your website, your URLs are often the first thing Google and customers will see. A friendly URL that is easy to read and includes words describing the content of the webpage can help visitors remember your website address and improve your website’s searching rank. For example, a company may use the URL “www.[company].com/service/” for the support section of their website, this is much easier to remember than a long convoluted URL like “www.[company].com/section/service/default.aspx?id=123&lan=en”.

In order to help every Magento 2 website have better URLs, we want to introduce a technique called URL rewrites that can turn unsightly URLs into friendly ones. Let’s see the detailed guide below to understand more!

What Are Magento 2 URL Rewrites?

URL Rewrite is a useful tool of Magento 2 platform that allows you to change any URL associated with a product, category, or CMS page. After enabling the Rewrite, any links that point to the previous URL are redirected to the new address. It’s can be said that URL rewriting is one of the best and quickest ways to improve user experience and search friendliness of websites.

Enable URL Rewrites

  • Log in to the Admin panel
  • Click Stores→Configuration→General→Web. 
  • Expand Search Engine Optimization
  • Set Yes for Use Web Server Rewrites field.
  • Click the Save Config button
  • Flush Cache



Set Up Automatic URL Redirects

After enabling URL Rewrites, you have to configure automatic URL Redirects that allow your Magento 2 store to automatically generate a permanent redirect whenever the URL key of any product is changed. Let’s follow the below steps:

  • Go to Stores→Configuration→Catalog→Catalog  
  • Expand Search Engine Optimization
  • Set Create Permanent Redirect for URLs if URL Key Changed to Yes
  • Click the Save Config button
  • Flush Cache



Change Product Page URLs

For products, please follow these steps:

  • You go to Catalog→Products, choose needed products
  • Expand the drop-down Search Engine Optimization
  • Update the URL Key
  • Save the changes



Change Category Page URLs

For Categories, please follow the below steps:

  • Go to Catalog→Categories, choose needed categories;
  • Expand the drop-down Search Engine Optimization
  • Update the URL Key
  • Save the changes



Change CMS Page URLs

For CMS page, please follow the below steps:

  • Go to Content→Pages, click Edit the page you need
  • Expand the drop-down Search Engine Optimization
  • Update the URL Key
  • Save the changes

Note: Well-optimized URLs are important for both search engine optimization and user experience. To have a friendly URL, please keep in mind that the URL should be short, include keywords, describe the content, use hyphens to separate words and use lowercase letters.

Add URL Rewrite And Create A Redirect Rule

There is another way to change URLs for products, categories and CMS pages. Let’s see the steps below: 

  • Go to Marketing→SEO&Search→URL Rewrites, a grid of redirect records will appear. 



  • Click on the Add URL Rewrite button to create new URL rewrites or click Edit to edit existing URL rewrites.
  • Configure the following settings:

Create URL Rewrite: Choose the option you need (Custom, For category, For product, For CMS page)

Store: Select the store view

Request Path: Input a new URL key and suffix (the Request Path is the original path)

Target Path: Enter the targetted path (the Target Path is the new path corresponding to the new web address)

Redirect Type: Choose either Temporary (302) or Permanent (301) redirect type 

– A 301 redirect sends the message to search engines that a website or page has been moved permanently.

– A 302 redirect lets search engines know that a website or page has been moved temporarily.

Description: Describe the rewrite

  • Click Save button



Note: In case you have hundreds or thousands of products that need rewriting, it will waste a lot of time. In order to save time, you can use Magento 2 Import Export URL Rewrites extension  to import URL Rewrites via a CSV file. Therefore, all URL Rewrites can be quickly updated at the same time and your process will be shortened in just a few minutes.

Make Magento 2 URL Rewrite Programmatically

Enabling the 301 is a useful tool to redirect any links to the previous URL to the new address. Let’s follow the steps below to accomplish this:

  • Generate a constructor file
* @var \Magento\UrlRewrite\Model\UrlRewriteFactory
protected $_urlRewriteFactory;

* @param Context $context
* @param \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
public function __construct(
    Context $context,
    \Magento\UrlRewrite\Model\UrlRewriteFactory $urlRewriteFactory
) {
    $this->_urlRewriteFactory = $urlRewriteFactory;
  • Create the 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 */
$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 configured URL redirects in your Magento 2 store.

Sum It Up

Let’s say goodbye with bad URLs and welcome friendly ones. Hope that this topic will give you lots of useful information and suggestions for your SEO strategy.

Write A Comment


( 1 ) Comment

  • Hadil

    thank you very nice website article