2

I want to log all 404 page in magento 2. Recently we have moved from woo-commerce to magento 2 and all the product/category/pages URL are now changed.

We want to log all 404 Page URL and take appropate action.

Suppose if a user clicked an old URL on search and got 404 on the website we want to log that url and want to redirect the user to corresponding new url for that page

So do anyone know the observer for 404 Event

4

Instead of observer use NoRouteHandler. By default all the 404 pages are forwarded to noroute.

etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\Router\NoRouteHandlerList">
    <arguments>
        <argument name="handlerClassesList" xsi:type="array">
            <item name="default" xsi:type="array">
                <item name="class" xsi:type="string">Vendor\Module\Controller\NoRouteHandler</item>
                <item name="sortOrder" xsi:type="string">200</item>
            </item>
        </argument>
    </arguments>
</type>
</config>

Vendor\Module\Controller\NoRouteHandler.php

<?php
namespace Vendor\Module\Controller;
class NoRouteHandler implements \Magento\Framework\App\Router\NoRouteHandlerInterface {

    protected $logger;
    public function __construct(\Psr\Log\LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function process(\Magento\Framework\App\RequestInterface $request) {
        //log the 404 url
        $this->logger->info(':: 404 list ::'.$request->getPathInfo());
        $request -> setModuleName('cms') -> setControllerName('noroute') -> setActionName('index');
        return false;
    }

}

output in system.log [http://your.host.name/tqertqwe/test1234qw.html]

[2017-07-24 14:33:47] main.INFO: :: 404 list ::/tqertqwe/test1234qw.html [] []

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.