Alter Header Status Response to 410 I have several urls with &quot;/tag/&quot; string; some of them have content and some of them don't, so they return 404.
How can I force them to return a 410 only for those that have no content?
I need a filter: has &quot;/tag/&quot; in URL and returns 404 -&gt; redirect to 410.
I tried in theme_preprocess_html(&amp;$variables) like this:
if ($pattern2){ //it has &quot;/tag/&quot; string
      $status = \Drupal::requestStack()-&gt;getCurrentRequest()-&gt;attributes-&gt;get('exception');      
      if ($status &amp;&amp; $status-&gt;getStatusCode() == 404){
          $response = new Response();
          $response-&gt;setStatusCode(410);
          return $response;
      }
}

It filters is working but nothing it's not redirecting to 410.
Thank you for your help!!
Yes, definitely higher up. When you are already inside of a 404 subrequest it is too late.
You could put the code in on404() of an HttpExceptionSubscriber, check the path and then replace the 404 with 410:
$event-&gt;setException(new HttpException(410));

See https://drupal.stackexchange.com/a/231263/47547