System: Magento 2.1.4 on 1and1 Cloud, CentOS

Searching throughout the web, I haven't found any specific help with this

I am wanting to change URLs in Magento2. There are URLs are like this: ( https://domain/page.html?background_color_name=510 ). I'd like it to read ( https://domain/page/pink-purple/ ). Selecting this URL displays products with this attribute.

What is the best practice of implementing this in Magento 2.1.4? If there is more than one way, GREAT!

Any assistance is appreciated.

FYI: There are over 10,000 products

1 Answer 1


You have to make a router that will catch requests for a specific route and use the additional url component as a request parameter.

For example, say you have a route call rewrite and you want requests for rewrite/pink-purple to be forwarded to rewrite/index/index with a request parameter named background that would be pink-purple. Here's what the router to do this might look like Some\Module\Controller\Router.php:


namespace Some\Module\Controller;

use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\RouterInterface;

class Router implements RouterInterface
    protected $actionFactory;

    public function __construct(ActionFactory $actionFactory)
        $this->actionFactory = $actionFactory;

     * @param RequestInterface $request
     * @return ActionInterface
    public function match(RequestInterface $request)
        $path = trim($request->getPathInfo(), '/');

        $parts = explode('/', $path);

        if ($parts && $parts[0] === 'rewrite') {

            if (isset($parts[1])) {
                $request->setParams(['background' => $parts[1]]);

            /** @var ActionInterface */
            return $this->actionFactory->create('Magento\Framework\App\Action\Forward');

        return null;

Now you have to add the router to the router list. In etc/frontend/di.xml you have this:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <type name="Magento\Framework\App\RouterList">
            <argument name="routerList" xsi:type="array">
                <item name="paramsRedirect" xsi:type="array">
                    <item name="class" xsi:type="string">Some\Module\Controller\Router</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">50</item>

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.