Magento 2 – Custom Module URL Rewrite

magento2moduleurl-rewrite

I have created a custom module for searching.
I am using JQuery for post url like below

var url = "/ymm?year="+year+"&make="+make+"&model="+model;
jQuery(location).attr('href',url);

my custom url created like below :

http://domainname.com/ymm?year=123&make=test&model=test1

But I want to create url like : /ymm/123-test-test1

I am using below code for rewrite url in root .htaccess file but it's not working. it's goes to catelogsearch result page.

RewriteCond %{REQUEST_URI} ^/ymm [NC]
RewriteCond %{QUERY_STRING} ^year=(.*)&make=(.*)&model=(.*)
RewriteRule (.*) http://domainname.com/ymm/%1-%2-%3? [R=301,L]

Please advise me.
Thanks.

Best Answer

Try following way:

VendorName/ModuleName/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="stackexchange" xsi:type="array">
                    <item name="class" xsi:type="string">VendorName\ModuleName\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">50</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

VendorName/ModuleName/Controller/Router.php


namespace VendorName\ModuleName\Controller;

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

    /**
     * Router constructor.
     *
     * @param \Magento\Framework\App\ActionFactory $actionFactory
     */
    public function __construct(
        \Magento\Framework\App\ActionFactory $actionFactory
    ) {
        $this->actionFactory = $actionFactory;
    }

    /**
     *
     * @param \Magento\Framework\App\RequestInterface $request
     * @return bool
     */
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $identifier = trim($request->getPathInfo(), '/');
        $d = explode('/', $identifier, 3);

        if(isset($d[0]) && ($d[0] != 'ymm')) {
            return false;
        }

        $paramStr = '';
        if(isset($d[1])) {
            $paramStr = $d[1];
        }

        $params = [];

        if($paramStr) {
            $params = explode('-', $paramStr);
        }


        $params = ['year' => $params[0], 'make' => $params[1], 'model' => $params[2]];

        $request->setModuleName('stackexchange')->setControllerName('index')->setActionName('index');
        if(count($params)) {
            $request->setParams($params);
        }

        $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier);

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