1

How can I create a custom error pages in my Magento 2 site. For example this pages: 500, 401, 403, and 404.

I tried for create block:

 $layout = $this->_view->getLayout();
 $block = $layout->createBlock('Training\Test\Block\Test');
 $this->getResponse()->appendBody($block->toHtml());

But not working return error

Catalog Layer has been already created

Below is my Controller

    class View{
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Catalog\Block\Product\ProductList\Toolbar $toolbar,
        PageFactory $resultPageFactory,
        \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
        \Magento\Framework\UrlInterface $url
    ) {
        $this->toolbar = $toolbar;
        $this->resultPageFactory = $resultPageFactory;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->url = $url;
    }

    public function beforeExecute(\Magento\Catalog\Controller\Category\View $category)
    {

        $product_limit = $category->getRequest()->getParam('product_list_limit');
        $available_limits = $this->toolbar->getAvailableLimit();
        if(!in_array($product_limit, $available_limits) && !empty($product_limit)) 
        {
            /*$norouteUrl = $this->url->getUrl('noroute');
            $category->getResponse()->setRedirect($norouteUrl);
            return;*/
            return $this->resultForwardFactory->create()->forward('noroute');
        }
    }
}

In that I want to replace return $this->resultForwardFactory->create()->forward('noroute'); with my custom error page URL

2
  • Can you add your all codes like xml, controller and block files? Mar 5, 2019 at 10:50
  • I get an error when editing a customer address. When I enter it into the system, when I save it, I get an error message "The requested URL has been rejected. For more information, please contact support and provide this reference ID: 170064036106aa7b54907f1469ee019". There was an error when running the test but localhost still works normally. Is there any solution? Thank you very much.
    – DatGold
    Nov 22, 2023 at 9:23

1 Answer 1

1

For 404 and 503 there are content pages available in Content > Pages so you can edit that page but 403 error page.

First You also have to add Fastly troubleshooting, if you are adding it till now.

https://devdocs.magento.com/guides/v2.2/cloud/cdn/trouble-fastly.html

To edit the WAF error page:

  1. Log in to the Magento Admin UI.
  2. Click Stores > Settings > Configuration > Advanced > System.
  3. In the right pane, expand Full Page Cache > Fastly Configuration > Custom Synthetic Pages.

enter image description here

  1. Click Edit WAF page.
  2. Complete the fields to update the HTML.

enter image description here

  • Status—Select the 403 Forbidden status.
  • MIME type—Type text/html.
  • Content—Edit the default HTML response to add custom CSS and update the title and messaging as needed.

    1. Click Upload at the top of the page to upload the customized HTML source to the Fastly server.
    2. Click Save Config at the top of the page to save the updated configuration file.
    3. Refresh the cache.

      • In the notification at the top of the page, click the Cache Management link.
      • On the Cache Management page, click Flush Magento Cache.

For Reference Magento Documentation: Customize error pages

5
  • Hii Full Page Cache > Fastly Configuration > Custom Synthetic Pages. This option not available in admin
    – Unknown
    Mar 5, 2019 at 11:30
  • @Parthavi which magento version you are using? Mar 5, 2019 at 11:37
  • Magento Version 2.2.5
    – Unknown
    Mar 5, 2019 at 11:41
  • Ok if you are using Magento 2 you have to setup Fastly Troubleshooting. Please check this devdocs.magento.com/guides/v2.2/cloud/cdn/trouble-fastly.html Mar 5, 2019 at 11:43
  • Fastly is available only in the Magento Commerce Cloud edition.
    – KrzysDan
    Mar 4, 2020 at 22:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.