Magento 2 – How to Rewrite Catalog/Search URL

magento-2.1magento2

Need to rewrite magento catalog search url catalogsearch/result?q=query so that it should be like katalog/resultater?q=query. As you can see before and after slash it should be different according to initial url.

Simply I need to translate this url. How to do this?

Best Answer

Yes,in this possible .You need to use Custom route in this case.

Create a custom route .

On custom route you can need it to katalog/resultate as controller as

Create di.xml

Create di.xml for define custom route in configuration

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2017 amitbera.com
 * created by Amit Bera(dev.amitbera@gmail.com)
 * Module is created for Custom Router demonstration
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\RouterList">
        <arguments>
            <argument name="routerList" xsi:type="array">
                <item name="katalog" 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">22</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

route file:

<?php
namespace [Vendorname]\[Modulename]\Controller;


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

    /**
     * Response
     *
     * @var \Magento\Framework\App\ResponseInterface
     */
    protected $_response;

    /**
     * @param \Magento\Framework\App\ActionFactory $actionFactory
     * @param \Magento\Framework\App\ResponseInterface $response
     */
    public function __construct(
        \Magento\Framework\App\ActionFactory $actionFactory,
        \Magento\Framework\App\ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->_response = $response;
    }

    /**
     * Validate and Match
     *
     * @param \Magento\Framework\App\RequestInterface $request
     * @return bool
     */
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        /*
         * We will search katalog/resultater and “exampletocms” words and make forward depend on word
         * -katalog/resultate will set front name to catalogsearch, controller path to result and action to index
         */
        $identifier = trim($request->getPathInfo(), '/');
        if(strpos($identifier, 'katalog/resultater') !== false) {

            $request->setModuleName('catalogsearch')->setControllerName('result')->setActionName('index');

        } else {
            //There is no match
            return;
        }

        /*
         * We have match and now we will forward action
         */
        return $this->actionFactory->create(
            'Magento\Framework\App\Action\Forward',
            ['request' => $request]
        );
    }
}