I have override url-rewrite router in my custom module,and add my custom rewrite code in override router file. after filter url created like below :


But I want to create url like :


I am using below code for rewrite url in router file .it's not working. it's goes to 404 result page.

    //custom code
  if ($request->getModuleName() === 'catalog' || $request->getModuleName() === 'cms') {
   $identifier = trim($request->getPathInfo(), '/');
    $urlKey_explode = explode('/', $identifier);

        $url_string = $urlKey_explode[1];

        //get manufacture
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        //Select product attribute from table
        $sql = "SELECT 
                    eav_attribute_option_value.option_id  , eav_attribute_option_value.value
                    join `eav_attribute_option` on `eav_attribute_option`.option_id = eav_attribute_option_value.option_id
                    eav_attribute_option.attribute_id = 83
                group by 
        $result_has_options_attribute_option = $connection->fetchAll($sql);
        foreach ($result_has_options_attribute_option as $key => $value)
            $productattriburtname = $value['value'];

            if(strpos($url_string, $productattriburtname) !== false){
                $manufacturerlabel = $value['value'];
                $url_string = str_replace($productattriburtname, "-", $url_string);
    $request->setParam('manufacturer', $manufacturerlabel);

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

    return $this->actionFactory->create(
        ['request' => $request]

    //end custom code

Please advise me.


You need to get the categoryId by url_key for your parameter and pass it to the forwarded action too. For the manufacturer you need to get the optionId which cou can do without SQL in your code by using EAV Config.

The following code should work and give you the category page with the manufacturer filter set, so the scope of this question should be answered with that. But anyway you will have to do some more changes in the filter of the categories since removing the manufacturer filter from your custom url will not work with Magento's standard logic. Also it would be good to add some error handling.

$identifier = trim($request->getPathInfo(), '/');
$urlKey_explode = explode('/', $identifier);
if (count($urlKey_explode) > 1){
    $urlKey = $urlKey_explode[0];
    $optionValue = $urlKey_explode[1];
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager

    //get the category id for the first given parameter
    $categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
    $categories = $categoryFactory->create()
    $categoryId = $categories->getFirstItem()->getEntityId();

    //get the option id for the second given parameter
    $eavConfig = $objectManager->get('Magento\Eav\Model\Config');
    $attribute = $eavConfig->getAttribute('catalog_product', 'manufacturer');
    $manufacturerOptionId = $attribute->getSource()->getOptionId($optionValue);

    //create forward with the correct settings
    $resultForwardFactory = $objectManager->get('Magento\Framework\Controller\Result\ForwardFactory');
    $result = $resultForwardFactory->create();
    $result->setParams(['id' => $categoryId, 'manufacturer' => $manufacturerOptionId]);

    return $result;

I hope that helps you. Feedback would be nice :-)

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.