Magento 2 – How to Redirect Page to 404

404-pagemagento2

I want to redirect sales/order/history page to 404 in magento2.

Best Answer

You can do using plugin.

Create around method on execute() over class Magento\Sales\Controller\Order\History and redirect to 404 page.

Plugin Class

<?php
namespace Stackexchange\Test\Plugin;


class HistoryPlugin
{
    /**
     * @var \Magento\Framework\App\Action\Context
     */
    private $context;
    private  $response;
    private  $redirect;
    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $url;


    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\UrlInterface $url
    )
    {
        $this->context = $context;
        $this->response = $context->getResponse();
        $this->redirect = $context->getRedirect();

        $this->url = $url;
    }

    public function aroundExecute(
        \Magento\Sales\Controller\Order\History $object,
        callable $proceed
    ){

        $norouteUrl = $this->url->getUrl('noroute');
        $this->getResponse()->setRedirect($norouteUrl);
        return;
    }
    /**
     * Retrieve response object
     *
     * @return \Magento\Framework\App\ResponseInterface
     */
    public function getResponse()
    {
        return $this->response;
    }
}
Related Topic