2

In magento 1 I can rewrite url in my custom module etc/config.xml like this:

<global>
        <rewrite>
            <digi_url>
                <from><![CDATA[/digisearcher\/feed.xml/]]></from>
                <to><![CDATA[digi/index/index/]]></to>
                <complete>1</complete>
            </digi_url> 
        </rewrite>
</global>

how can I achieve this in my Magento2 custom module?

2
+25

You can add a custom Router class to match your criteria and then redirect to your desired path

app/code/Vendor/ModuleName/etc/di.xml

<?xml version="1.0"?>
<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_routest" xsi:type="array">
                    <item name="class" xsi:type="string">Vendor\ModuleName\Controller\Router\CustomRouter</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">20</item>
                </item>
            </argument>
            <argument name="cacheable" xsi:type="boolean">false</argument>
        </arguments>
    </type>
</config>

app/code/Vendor/ModuleName/Controller/Router/CustomRouter

<?php
namespace Vendor\ModuleName\Controller\Router;

use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ResponseInterface;

class CustomRouter implements \Magento\Framework\App\RouterInterface
{   

    private $actionFactory;
    private $response;

    public function __construct(
        ActionFactory $actionFactory,
        ResponseInterface $response

    ) {
        $this->actionFactory = $actionFactory;
        $this->response = $response;
    }

    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $identifier = $request->getOriginalPathInfo(); //to read the route
        /** Apply Url logic here */
        return $this->_response->setRedirect('route/controller/action'); //redirect 
    }

}
| improve this answer | |
0

As per my knowledge, we don't have that option in magento 2. Did you try the standard url rewrite option in admin panel?

You can do it in controller level by extending the controller OR can do it in .htaccess file level Or via routes.yaml in case if you are using magento cloud.

| improve this answer | |
0

For the custom URL rewrite magento 2 allowed us the concept of the custom router, you need to implement that things into your custom extension like the Magento CMS module do.

  1. app\code\Vendor\Extension\etc\frontend\di.xml
<?xml version="1.0"?>
<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="customsearch" xsi:type="array">
                    <item name="class" xsi:type="string">Vendor\Extension\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">50</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>
  1. app\code\Vendor\Extension\Controller\Router.php
<?php
namespace Vendor\Extension\Controller;

class Router implements \Magento\Framework\App\RouterInterface
{
    /**
     * @var \Magento\Framework\App\ActionFactory
     */
    protected $actionFactory;

    public function __construct(.....
        \Magento\Framework\App\ActionFactory $actionFactory,
        .....) {
        $this->actionFactory = $actionFactory;
    }


    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $identifier = trim($request->getPathInfo(), '/');

       //PUT YOUR CUSTOM LOGIC HERE FOR THE URL


        $request->setModuleName('digi')->setControllerName('index')->setActionName('index');
        // if you want to pass any parameter ->setParam('variable', $value);

        return $this->actionFactory->create(\Magento\Framework\App\Action\Forward,['request' => $request]);
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.