0

Anyone knows an alternative way to implement Fancy URL(This won't affect url_rewrite table entries) for Magento 2, as we had for Magento 1 as mentioned here: http://excellencemagentoblog.com/blog/2011/10/29/simple-url-rewrite-using-magento-xml/

I need to inject product SKU this way: http://example.com/book/sku/xyz I've implemented it in one of my Magento 1 project using Fancy URL concept, but couldn't find an alternative for Magento 2. Your help would be appreciated.

1 Answer 1

3

Yes, you can achieve using custom router in Magento 2. Here is well-explained documentation regarding your requirement: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html


If you wany fancy URL for catalog pages, you can also achieve uit using plugin.

In your custom module, Create di.xml file under etc/frontend folder and write below plugin declaration there:

<type name="\Magento\UrlRewrite\Controller\Router">
    <plugin name="mycompany_seo_friendly_url" type="Mycompany\Seo\Plugin\Controller\Router"/>
</type>

Now, create a plugin class named Router.php at Mycompany\Seo\Plugin\Controller\Router location with below content:


namespace Mycompany\Seo\Plugin\Controller;

class Router
{

    /**
     * @param \Magento\UrlRewrite\Controller\Router $subject
     * @param $request
     * @return null
     */
    public function beforeMatch(
        \Magento\UrlRewrite\Controller\Router $subject,
        $request
    ) {
        $identifier = trim($request->getPathInfo(), '/');
        if (strpos($identifier, 'sku') !== false) {
            $urlRequest = $this->getKeyByString($identifier, "sku");
            $productSku = $this->getValueByKey($identifier, "sku");
            $request->setPathInfo($urlRequest);
            if($productSku != null) {$request->setParam('sku', $productSku);}
        }
        return null;
    }

    public function getValueByKey($string, $key)
    {
        if (strpos($string, "/" . $key . "/") !== false){
            $urlArray = explode("/" . $key . "/", $string);
            if (count($urlArray) > 0) {
                $value = $urlArray[1];
                if (strpos($urlArray[1], '/') !== false) {
                    $value = substr($urlArray[1], 0, strpos($urlArray[1], '/'));
                }
                return $value;
            }
        }
        return null;
    }

    public function getKeyByString($string, $key)
    {
        if (strpos($string, "/" . $key . "/") !== false) {
            $urlArray = explode("/" . $key . "/", $string);
            if (isset($urlArray[0]) && $urlArray[0] != "") {
                return $urlArray[0];
            }
        }
        return null;
    }
}

3
  • Thank you for your reply. Let me check. Commented Sep 5, 2019 at 10:05
  • It's work for me. Thank you so much. :-) Commented Sep 5, 2019 at 10:41
  • Happy to help @PratikMehta :) Commented Sep 5, 2019 at 10:53

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.