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

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

 

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

 

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

 

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

 

Add a Block – Block/Response/Gone.php

 

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

 

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