Site icon MageComp Blog

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!
Exit mobile version