Magento URL rewrites: How to create it in Magento 2

Magento URL rewrites help store owners to insert high-value keywords that support better for the product index by search engines. Moreover, URL rewrite comes into play when you want to change the URL of a product, catalog, or CMS page or redirect one page to another. More often than not, Magento merchants need it in order to make their URLs fancy & more search engine friendly, i.e. searchable and readable.  In this article, we will introduce everything you need to know about the Magento URL rewrites and how to create them in Magento 2.

What are Magento URL rewrites?

URL rewrites

Magento URL Rewrite is a useful tool 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 the Magento URL rewrite is one of the best and quickest ways to improve user experience and search friendliness of websites.

Benefits Of Magento 2 URL Rewrites

Sometimes, you may wonder if a friendly URL is essential to your Magento 2 store.  The answer is: absolutely! When it comes to your online store, your URLs are often the first thing Google and shoppers will see. A friendly URL that is simple to read and contains words describing the content of the page can assist users to remember your website address and boost your website’s search rank. Therefore, URL rewrites are essential to SEO maintenance practices, their influence is far bigger and wider than that.  Here are some benefits of Magento 2 URL Rewrites:

  • Simplicity: Once you rewrite the URL, it enables you to show shorter and to-the-point URLs since long and intricate ones distract the attention rather than capture it. This provides visitors the wrong impression about your website
  • Boost SEO: Also, Magento 2 URL Rewrites allows the shop owners to technically change the site’s hierarchy while still protecting the current URLs of the website. This helps you to enhance search engine optimization.
  • Keywords are a crucial element to a page’s ranking position on Google search results. Therefore, rewriting URLs will supports SEO specialists to manage the keywords within URLs no matter what technical contemplations. More importantly, this technique makes it easier for search engines to understand.
  • Improve the site visibility: The average length of URLs kept in the top 10 results is 36 characters. Hence, rewriting the URLs and shortening them would dramatically boost their rankings and site visibility.

Now, that we understood what the Magento URL rewrites are, it is time to get back to our main topic today, we will learn to configure Magento URL rewrites. There are two methods to enable URL rewrites in Magento 2.

Method 1: Enable Magento 2 URL Rewrites via the Admin Panel

First, Step 1: Follow the below steps

  • You log in to the Admin Panel
  • You move to Stores > Configuration > General > Web
  • Choose the drop-down menu of the Search Engine Optimization section
  • Set the Use Web Server Rewrites to ‘Yes’.
  • Save your settings from the “Save Config” button.
Admin Panel

Next, Step 2: Configure automatic URL Redirect

  • Navigate to Stores > Configuration > Catalog > Catalog
  • Choose the drop-down menu of the Search Engine Optimization section
  • Change Create Permanent Redirect for URLs if URL Key Changed to ‘Yes’
  • Save your settings from the “Save Config” button.
Magento URL rewrites

Then, Step 3: Change product or category page URLs

  • Move to Products > Catalog, choose the products for which the URLs have to be rewritten
  • Choose the drop-down menu of the Search Engine Optimization section
  • Update the URL Key here, and use only lowercase characters
  • Save the changes and refresh the cache.
  • Configuring these settings will give you the desired results. The users will be redirected to the new web page from the old URL.
Magento URL rewrites

Continue with Step 4: See Magento 2 URL Rewrite table

You go to Marketing > SEO&Search > URL Rewrites in Admin Panel. Here you will see the redirect records. As shown in the image below, the most recent redirects will display at the top of the Magento 2 URL Rewrites table.

 Rewrite table

Finally, Step 5: Add URL Rewrite using the Add URL Rewrite button

In this step, you move to Marketing> SEO&Search > URL Rewrites. If you have used the instructions above, then you are in the required tab. Click the “Add URL Rewrite” button. Configure the settings:

  • Generate URL Rewrite: Select the custom option for the category, product, or CMS page.
  • Store: Choose the store view
  • Request Path: Input a new URL key and suffix for the category of the product
  • Target Path: Enter the targetted path
  • Redirect Type: Select either Temporary (302) or Permanent (301) redirect type
  • Description: Describe the rewrite
  • Save your settings
  • Lastly, you fulfill reindex via the backend or follow this command: PHP bin/Magento indexer: reindex in the case where the adjustments do not reflect in the result.
Programmatically

Method 2: Add URL Rewrite Programmatically

Magento URL rewrites are one of the awesome solutions for shop owners who want to really create a huge amount of traffic to your website. The purpose of rewriting the URL is to enable you to create 301 redirects into Magento 2 in a programmatical way. Let’s follow the steps below to create Magento URL rewrites programmatically in Magento 2.

Step 1: Generate constructor file

<?php 
/**
 * @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);
}

Step 2: Fill in Custom Magento URL rewrites 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();

Congratulations! You have succeeded in creating Magento URL rewrites programmatically in your Magento 2 store.

Conclusion

That’s all about creating a URL rewrite in Magento 2. We hope that you found this tutorial helpful. However, if you find anything unclear about these solutions or any other procedure related to this method, then feel free to contact us to experience our Magento development services. We’re very pleased to support you.

Magento get current URL: Method to get current URL and base URL in Magento 2