You can use Request Routing in this case by do the following steps:
- 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>
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;
}
}
Run this command:
php bin/magento s:up
Enter your url like www.example.com/var1/var2 and see the result