2

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?

2
  • If i want to remove catalogsearch/result?q= and only query should exist. like /query. how to achieve this? Commented Jun 12, 2017 at 6:26
  • Do you mean http//www.site.url/query ? Imagine that you might have product named "query" and addresses could be similar. So that's the conflict and this is nonsense.
    – white-imp
    Commented Jun 13, 2017 at 7:58

2 Answers 2

2

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([email protected])
 * 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]
        );
    }
}
0

If you want to integrate the 'q' parameter within the rewrite use the below code for the controller

This will make the url search-for-something-in-my-shop.php return catalogsearch/result/?q=something

Just like a .htaccess reWriteRule

<?php

namespace Vendor\Module\Controller;


class Router implements \Magento\Framework\App\RouterInterface
{
    private $actionFactory;

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

    /**
     * Router constructor.
     * @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;
    }

    /**
     * @param \Magento\Framework\App\RequestInterface $request
     * @return \Magento\Framework\App\ActionInterface|null
     */
    public function match(\Magento\Framework\App\RequestInterface $request)
    {

        $identifier = trim($request->getPathInfo(), '/');
        if(strpos($identifier, '-in-my-shop.php') !== false && strpos($identifier, 'search-for-') !== false) {

            $searchString = str_replace("replacement-","",$identifier);
            $searchString = str_replace("-batteries.php","",$searchString);

            $request->setModuleName('catalogsearch')->setControllerName('result')->setActionName('index')->setParam('q',$searchString );

        } 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]
        );

        return null;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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