How to add Prefix to Store Products URLs in Magento 2

How to Add Prefix To Product Url in Magento2

There is no question that a search engine has changed the game for every store owner whether you have an online or offline business. And this search engine became part of our life the way it provides a solution for any query. In this digital era, if you want to grow more, earn more and to acquire tons of customers, the search engine is the only key to success for the store owner to expand their business without land boundaries. We can say that SEO is a scientific art that consists of universal efforts. And you are not alone in results there thousand, millions and billions of other pages same things. To stand out in the result page, you need to optimize your store, products, and everything that is public and search engine can crawl.

By default Magento 2 generates product url like
https://magecomp.com/magento-2-indian-gst.html
<——- Store name—->/<—-product name—>.html

But sometimes we need to customize this URL for building SEO friendly URL or to organize store products in a better way so it becomes to understand like
https://magecomp.com/magento-2/magento-2-indian-gst.html
<——- Store name—->/<– prefix –>/<—product name—>.html

And the URL extension will be added as per your backend configuration. This makes sense when it comes to SEO and optimizing store. To do the same, follow three steps guide to add a prefix to Magento 2 product page URL.

First, we need to create ‘di.xml’ at the following location and add below code.
app\code\Vendor\Extension\etc\di.xml



	
   

After that you need to create one more file that will fetch default Magento URLs so you can override those urls easily. For that purpose you need to create ‘Url.php’ inside your custom extension folder path.
app\code\Vendor\Extension\Model\Url.php


app\code\Vendor\Extension\Controller\Router.php

You can use this code according to your need for generating different custom product URL.
If you need any help regarding this code? Simply leave a comment and if this found it helpful smash that stars.
Happy Generating!
Previous Article

This Halloween, Enjoy Scary savings! Up to 40% OFF across all Paid Magento Extensions

Next Article

Diwali Offer, Save upto 40% off on Magento Paid Extensions & Services

View Comments (13)
  1. Hello! I would like to do the same thing for categories.
    What should I modify?
    Thank you!

    1. For that one you will need to modify the code accordingly, and you can identify the given URL is for the category to achieve it.

  2. I’m having trouble using these codes, I followed the tutorial and it didn’t work, I wanted to know if you can pass me the files to download, I see that it’s like a module? what did you do, thanks!

  3. Hi,

    It is working fine in single default website and single store, but I have 2 website and each website have multiple stores, Issue is that product url key is not showing, In list page url is showing till static not complete.

  4. $productUrl = $this->getUrlInstance()->setScope($product->getStoreId())->getUrl(‘ ‘,$params); line is not getting product url in my case I have multiple website and each website have multiple store

  5. Hi Mage comp ,
    i have add the custom prefix successfully i can not add the products when I click on add to cart then page redirect to search the custom prefix of the current product ,I am not able to add products

  6. first all i want to tell that di.xml file is not having any code in it .
    secondly i used this code
    in my di.xml file .
    third it is throwing this error Exception #0 (LogicException): Front controller reached 100 router match iterations

    Exception #0 (LogicException): Front controller reached 100 router match iterations

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨