Magento 2 How to modify HTTP Header of a Page?

Sometimes you want a Search Engine to know that an URL is Gone. To do so you have to modify the HTTP Header of the Page.

In this article you get a couple snippets with an explanation how you can modify the headers of a page by using the Magento 2 Forward functionality.

Cool feature in the Mage2 Module Experius PageNotFound which can be found on Github

enter image description here

For more information see the following commit

additional commit

I recommend to use Mage2Gen to generate your module for you!

Basic module with Mage2Gen for you!

Based on this commit you can implement is as followed:

Add a frontend route – etc/frontend/routes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route frontName="vendor_module" id="vendor_module">
            <module name="Vendor_Module"/>


Add a frontend event – etc/frontend/events.xml

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_action_predispatch_cms_noroute_index">
        <observer instance="Vendor\Module\Observer\Controller\ActionPredispatch" name="vendor_module_observer_controller_actionpredispatch_controller_action_predispatch"/>


Create the Observer and implement this function – Observer/Controller/ActionPredispatch.php

 * @var \Magento\Framework\Controller\ResultFactory 
private $resultFactory;

protected function redirect($url)

        $result = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_FORWARD);
        return $result->setModule('experius_pagenotfound')->setController('response')->forward('gone');



Then add a Controller – Controller/Response/Gone.php

namespace Vendor\Module\Controller\Response;
class Gone extends \Magento\Framework\App\Action\Action
    protected $resultPageFactory;
     * Constructor
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
     * Execute view action
     * @return \Magento\Framework\Controller\ResultInterface
    public function execute()
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setStatusHeader(410, '1.1', 'Gone');
        $resultPage->setHeader('Status', '410 Gone');
        return $resultPage;


Add a Block – Block/Response/Gone.php

namespace Vendor\Module\Block\Response;
class Gone extends \Magento\Framework\View\Element\Template
    protected function _prepareLayout()
        $this->pageConfig->getTitle()->set('410 Gone');
        $pageMainTitle = $this->getLayout()->getBlock('page.main.title');
        $pageMainTitle->setPageTitle(__('Whoops, our bad...'));
        return parent::_prepareLayout();


Update the frontend with layout xml – view/frontend/layout/vendor_module_response_gone.xml

<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Response\Gone" name="response.gone" template="Vendor_Module::response/gone.phtml"/>


And finally add a template – view/frontend/templates/response/gone.phtml

    <dt><?php echo __('The page you requested was not found, and we have a fine guess why.'); ?></dt>
        <ul class="disc">
            <li><?php echo __('If you typed the URL directly, please make sure the spelling is correct.'); ?></li>
            <li><?php echo __('If you clicked on a link to get here, the link is outdated.'); ?></li>