How To

How to add Prefix to Store Products URLs in Magento 2

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!
Click to rate this post!
[Total: 15 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • 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

  • 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

  • $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

  • 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.

  • 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!

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

14 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago