0

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.

3

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]
        );
    }
}
|improve this answer|||||
  • Thank you so much. It's work for me. – Prakash Sep 5 '17 at 17:51
  • Don't forget to accept this answer , it will help others – Sohel Rana Sep 5 '17 at 21:07
  • Perfect! I was just looking for this one. The hardest part of every problem solving is to find out how to cal it. This one is routing. Official doc is devdocs.magento.com/guides/v2.3/extension-dev-guide/… – Jaro Mar 5 '19 at 15:06
  • works perfect, thanks – Sameer Bhayani Apr 7 at 12:07

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.