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
  • 1
    This should be done higher up the chain than a preprocess hook.
    – Kevin
    Jul 5, 2020 at 18:10

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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