Question

I am trying to modify HTTP header for current URL. I want to set 410 status if current page header is 404.

below code I used but I have created new controller so it always set 410 for that controller but I want to set for current URL.

    $resultPage = $this->resultPageFactory->create();

    $resultPage->setStatusHeader(410, '1.1', 'Gone');
    $resultPage->setHeader('Status', '410 Gone');

    return $resultPage;

I am not sure which function used to set Http header. Has anyone idea?

Was it helpful?

Solution

This is implemented in the Mage2 Module Experius PageNotFound which can be found on Github

https://github.com/experius/Magento-2-Module-PageNotFound

enter image description here

For more information see the following commit

https://github.com/experius/Magento-2-Module-PageNotFound/commit/d3332a4f43122f6532617ae57aab8f1d6380a512#diff-6c0df54a0adc7658411e157ba9feaf3a

additional commit

https://github.com/experius/Magento-2-Module-PageNotFound/pull/19/commits/7d230cb6352b16633ed2d32dde28d7527e8eff3a

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

Basic module with Mage2Gen for you!

https://mage2gen.com/load/8cfec17a-a5ff-481e-9f36-2091e0237006

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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route frontName="vendor_module" id="vendor_module">
            <module name="Vendor_Module"/>
        </route>
    </router>
</config>

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

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"/>
    </event>
</config>

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

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

protected function redirect($url)
{

    if($url=='410'){
        $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

<?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;
        parent::__construct($context);
    }
    /**
     * 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

<?php
namespace Vendor\Module\Block\Response;
class Gone extends \Magento\Framework\View\Element\Template
{
    protected function _prepareLayout()
    {
        $this->pageConfig->addBodyClass('410');
        $this->pageConfig->getTitle()->set('410 Gone');
        //$this->pageConfig->setKeywords();
        //$this->pageConfig->setDescription();
        $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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Response\Gone" name="response.gone" template="Vendor_Module::response/gone.phtml"/>
        </referenceContainer>
    </body>
</page>

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

<dl>
    <dt><?php echo __('The page you requested was not found, and we have a fine guess why.'); ?></dt>
    <dd>
        <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>
        </ul>
    </dd>
</dl>

Let me know if you have any questions!

OTHER TIPS

I think you first you need to clear header

you can inject this dependency into your __construct

   Magento\Framework\HTTP\PhpEnvironment\Response

and then clear header using clearHeader($name) method

and then add header by using this code

    $header->getHeaders()->addHeaderLine($name, $value);

you can find this code sample in this class only

410 != 404

Don't do this. It's a very bad idea.


Nevertheless... You didn't point out what server you are using but I'd use your servers abilities to change the code from 404 to 410. Aka with rewrite conditions. Consult your server manual.

Check this


 /**
 * @var \Magento\Framework\View\Result\PageFactory
 */
protected $resultPageFactory;

/**
 * @param \Magento\Backend\App\Action\Context $context
 * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
 */
public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    parent::__construct($context);
    $this->resultPageFactory = $resultPageFactory;
}

/**
 * Noroute action
 *
 * @return \Magento\Backend\Model\View\Result\Page
 */
public function execute()
{
    /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
    $resultPage = $this->resultPageFactory->create();
    $resultPage->setStatusHeader(404, '1.1', 'Not Found');
    $resultPage->setHeader('Status', '404 File not found');
    $resultPage->addHandle('adminhtml_noroute');
    return $resultPage;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top