2

I have several urls with "/tag/" 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 "/tag/" in URL and returns 404 -> redirect to 410.

I tried in theme_preprocess_html(&$variables) like this:

if ($pattern2){ //it has "/tag/" string
      $status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');      
      if ($status && $status->getStatusCode() == 404){
          $response = new Response();
          $response->setStatusCode(410);
          return $response;
      }
}

It filters is working but nothing it's not redirecting to 410.

Thank you for your help!!

  • 1
    This should be done higher up the chain than a preprocess hook. – Kevin Jul 5 at 18:10
5

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->setException(new HttpException(410));

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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