Skip to content

Redirect 404 Pages to HomePage

I am redirecting 404 pages to the homepage, but the URL is not changing it’s the same 404 page URL I want to change it to the homepage.

My code :

<?php
namespace VendorModuleAppRouter;
class NoRouteHandler implements MagentoFrameworkAppRouterNoRouteHandlerInterface
{
public function process(MagentoFrameworkAppRequestInterface $request)
{
$requestValue = ltrim($request->getPathInfo(), '/');
$request->setParam('q', $requestValue);
$request->setModuleName('cms')->setControllerName('index')->setActionName('index');
return true;
}
}

etc/frontend/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoFrameworkAppRouterNoRouteHandlerList">
        <arguments>
            <argument name="handlerClassesList" xsi:type="array">
                <item name="extension" xsi:type="array">
                    <item name="class" xsi:type="string">VendorModuleAppRouterNoRouteHandler</item>
                    <item name="sortOrder" xsi:type="string">80</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>