4

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?

4
  • can u explain which url set ?
    – Abdul
    Commented Jan 6, 2018 at 6:52
  • @Abdul I added new option 410 in redirect type apart from 301 and 302. Now i want to setup header 410 for those url but its always take 302
    – Kul
    Commented Jan 7, 2018 at 14:54
  • @Kul I have just updated my answer with an example, let me know if you need any more help!
    – Mr. Lewis
    Commented Jan 10, 2018 at 17:24
  • Hi if check my answere if you are user result redirect then use extend \Magento\Backend\App\Action $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setStatusHeader(403, '1.1', 'Forbidden'); return $resultRedirect->setPath('*/auth/login'); Commented Jan 11, 2018 at 10:55

4 Answers 4

4
+100

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!

6
  • @mr-lewis I've already use this module. but its redirect to new page and old page url show 302. but i want both url should be 410
    – Kul
    Commented Jan 11, 2018 at 12:39
  • @Kul thanks for your feedback I have just created a fix for this in a pull request github.com/experius/Magento-2-Module-PageNotFound/pull/19
    – Mr. Lewis
    Commented Jan 11, 2018 at 14:36
  • @Kul Changes have been made in the answer in the Observer method | Create the Observer and implement this function - Observer/Controller/ActionPredispatch.php
    – Mr. Lewis
    Commented Jan 11, 2018 at 14:40
  • @Kul fixes have been merged! And new tag has been added 1.2.5
    – Mr. Lewis
    Commented Jan 11, 2018 at 17:44
  • Great work @mr.lewis. Its working fine.
    – Kul
    Commented Jan 12, 2018 at 8:02
1

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

0

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.

0
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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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