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


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