How to add custom error pages in Magento 2?

The question:

I’m having a problem since I’m new to Magento.

how can I create a custom error pages in my Magento 2 site?

For example these pages: 500, 401, 403, and 404. I hope someone can help me.

Thanks

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

you can customize (404) page from admin

Content->Pages->404 Not Found->Edit->content

here you can change whatever content you need.

If you look programmatically solution, add the below content in your <root-magent>/app/code/Vendor/Module/etc/di.xml

<type name="MagentoFrameworkAppRouterNoRouteHandlerList">
<arguments>
    <argument name="handlerClassesList" xsi:type="array">
        <item name="default" xsi:type="array">
            <item name="class"
            xsi:type="string">VendorModuleControllerNoRouteHandler</ite
            m>
            <item name="sortOrder" xsi:type="string">200</item>
        </item>
    </argument>
</arguments>
</type>

add this content in above mentioned controller

<?php
namespace VendorModuleController;
class NoRouteHandler implements MagentoFrameworkAppRouterNoRouteHandlerInterface {

    public function process(MagentoFrameworkAppRequestInterface $request) {
        $moduleName= 'cms';
        $controllerName = 'index';
        $actionName= 'index';

        $request
        ->setModuleName($moduleName)
        ->setControllerName($controllerName)
        ->setActionName($actionName);
        return true;
    }
}

In this controller you can set whatever module/controller/actionname for 404 page.

follow this link to set 401,403,500 page


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment