0

I have a URL that looks like this:

site.local/wheel-tyres/tyres.html?find=aprilia-af-1-futura-1989-4729&sid=T80yDaUX1a

I'm trying to get it to look like this:

site.local/wheel-tyres/tyres/aprilia-af-1-futura-1989-4729/

I found this post on StackOverflow and tried to implement with amendments I though would work, but the page 500s now.

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^*.html/(.*)$ /*.html?url=$1 [L,QSA]

This code was added to the pub/.htaccess file.

How do I go about getting the find param into a readable url?

4
  • Did you make all needed setup related to seo url in admin panel?
    – Kirill
    Jan 22, 2019 at 9:26
  • @KirillKorushkin not sure I understand - could you please try to clarify? :)
    – treyBake
    Jan 22, 2019 at 9:31
  • are you suing nginx or apache? Jan 22, 2019 at 10:07
  • @PhilippSander Apache :)
    – treyBake
    Jan 22, 2019 at 10:08

2 Answers 2

7
+100

By default, Magento2 has 4 routers Base > CMS > URL Rewrite > Default In your case, you should create a custom router. I can suggest you create a custom module and then create a custom router to solve your issue. You can do as the following steps:

  1. Create app/code/Vendor/Module/registration.php
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);
  1. Create app/code/Vendor/Module/etc/module.xml
<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="0.0.1">
    </module>
</config>
  1. Create app/code/Vendor/Module/etc/frontend/di.xml
<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <type name="Magento\Framework\App\RouterList">
    <arguments>
        <argument name="routerList" xsi:type="array">
            <item name="custom_router" xsi:type="array">
                <item name="class" xsi:type="string">Vendor\Module\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">70</item>
            </item>
        </argument>
    </arguments>
</type>
</config>
  1. Create app/code/Vendor/Module/Controller/Router.php
<?php

namespace Vendor\Module\Controller;


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

    /**
     * Router constructor.
     * @param \Magento\Framework\App\ActionFactory $actionFactory
     */

    public function __construct(\Magento\Framework\App\ActionFactory $actionFactory)
    {
        $this->actionFactory = $actionFactory;
    }

    /**
     * @param \Magento\Framework\App\RequestInterface $request
     * @return \Magento\Framework\App\ActionInterface|null
     */
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $pathInfo = $request->getPathInfo();
        $find = $request->getParam('find', false);
        if ($find && preg_match("%^/.*/.*\.html$%", $pathInfo, $m)) {
            $pathInfo = str_replace('.html', '', $pathInfo);
            $pathInfo = $pathInfo . '/' . $find . '/';
            $request->setPathInfo($pathInfo);
            return $this->actionFactory->create('Magento\Framework\App\Action\Forward',
                ['request' => $request]);
        }
        return null;
    }
}
  1. Install new module by this command:

php bin/magento setup:upgrade

This module will support to convert the url like

site.local/wheel-tyres/tyres.html?find=aprilia-af-1-futura-1989-4729/

to

site.local/wheel-tyres/tyres/aprilia-af-1-futura-1989-4729/

Hope this will solve your issue

Cheers

3
  • wait - should be the other way round: site.local/wheel-tyres/tyres.html?find=aprilia-af-1-futura-1989-4729/ to site.local/wheel-tyres/tyres/aprilia-af-1-futura-1989-4729/
    – treyBake
    Jan 23, 2019 at 8:44
  • Sorry, my mistake. I've updated my answer. Please check it. Thanks
    – bachlee89
    Jan 24, 2019 at 2:48
  • I have up-voted your answer, It seems correct Jan 24, 2019 at 4:35
-1

Configure URL Rewrites in Magento 2

  • On the Admin Panel, Stores > Settings > Configurations.
  • In the left panel, under General, select Web.
  • Open the Search Engine Optimization section.
  • Select "YES"
  • Clear configuration cache

source

1
  • it's already set to YES
    – treyBake
    Jan 22, 2019 at 10:13

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.