-1

I want to load my magento store on www.example.com/var1/var2 Instead of www.example.com and i want my products url like www.example.com/var1/var2/product-url Var1 & var2 can be dynamic variables. Help me to rewrite my urls in maganeto 2.2.6

What I have tried is developing a custom module but now all I have to do is to load all magento modules in my custom module. It seems am working with totally custom in magento which is not a good practice. By this way I have to reinitialize my all magento modules into my custom module. By this way magento is useless for me by using this approach.

  • I'm voting to close this question as off-topic because Stack Overflow is a [programming-related](http://stackoverflow.com/help/on-topic) Q&A site. Your question is not about programming. Perhaps you should post it on http://magento.stackexchange.com instead? – Enigmativity Dec 17 '18 at 10:35

1 Answers1

1

You can use Request Routing in this case by do the following steps:

  1. create di.xml under YourVendor/YourModule/etc/di.xml with this content:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\App\RouterList"> <arguments> <argument name="routerList" xsi:type="array"> <item name="custom_router" xsi:type="array"> <item name="class" xsi:type="string">YourVendor\YourModule\Controller\Router </item> <item name="disable" xsi:type="boolean">false</item> <item name="sortOrder" xsi:type="string">70</item> </item> </argument> </arguments> </type> </config>

  1. Create Router class that implement RouterInterface as below:

    class Router implements \Magento\Framework\App\RouterInterface { private $actionFactory;

    /**
     * Router constructor.
     * @param \Magento\Framework\App\ActionFactory $actionFactory
     */
    
    public function __construct(\Magento\Framework\App\ActionFactory $actionFactory)
    {
        $this->actionFactory = $actionFactory;
    }
    
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $info = $request->getPathInfo();
        if (preg_match("%^/(var1/var2)(.*?)$%", $info, $m)) {
            $request->setPathInfo(str_replace('var1/var2', '', $info));
            return $this->actionFactory->create('Magento\Framework\App\Action\Forward',
                ['request' => $request]);
        }
        return null;
    }
    

    }

  2. Run this command:

    php bin/magento s:up

Enter your url like www.example.com/var1/var2 and see the result

bachlee89
  • 717
  • 4
  • 8
  • I have tried this and its working already. But its loading home page only. I want that all my urls like my product hrefs and my cart page link should be converted into www.example.com/var1/var2/my-cart or www.example.com/var1/var2/my-product – Abdulrehman Sheikh Dec 17 '18 at 11:23
  • I don't want to change base_url because var1 and var2 are dynamic and I do not want to update the database again and again for every single user with different var1/var2 – Abdulrehman Sheikh Dec 17 '18 at 14:00
  • var1/var2 are basically var1 is type and var2 is vendor name so both can be change again and again – Abdulrehman Sheikh Dec 17 '18 at 14:00