We set up a custom error page theme by creating a block with the error text and then used the following code within a hook_theme_suggestions_page_alter implementation to determine when to override the template and show that "Page Not Found" block:

// error page template suggestions
if (!is_null(Drupal::requestStack()->getCurrentRequest()->attributes->get('exception'))) {
  $status_code = Drupal::requestStack()->getCurrentRequest()->attributes->get('exception')->getStatusCode();
  $suggestions[] = 'page__error';

Now it turns out that due to some hosting constraints we need to be able to serve up a version of the "Page Not Found" page with an HTTP status code of 200. So, is it possible to use response()->setStatusCode() within hook_theme_suggestions_page_alter? I was hoping to just override the status code for a single arbitrary route, but so far my attempts to call it from there have been unsuccessful.

Admittedly, it would be a hack to call it there even if I can, but I wanted to check before reverting a bunch of theming code.

1 Answer 1


You don't need this code by the way, it is in core since D8.8.x, see https://www.drupal.org/node/2960810.

If you want to change the status code then do this in an event subscriber. If you want Drupal to build a normal 404 page and only change the status code of the already built error page then use a response event subscriber and set the status code:


See this answer for the scaffolding https://drupal.stackexchange.com/a/201297/47547

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.