'Return Status Code 410 with Symfony for Google Bot

As Google Bot is visiting one of our websites with wrong URLs, I constantly get Server errors bugging me as Administrator (The reason is that I accidentially submitted a wrong sitemap 3 months ago. Yes, I have resubmitted, deleted, resubmitted several times but it's still visiting).

I now learned in the Google Forum that using a 410 response will make the goggle bot stop visiting.

Does anyone have an idea how to send a 410 response from Symfony2? If that won't work, I would also be fine with a plain PHP-solution.

Thanks a lot for any help.



Solution 1:[1]

I have tried this, seems to work. Will update once I learn whether it has a long-term effect

   $response = new Response();
   $response->headers->set('Content-Type', 'text/html');
   $response->setStatusCode(410);
   return $response;}

Solution 2:[2]

That's basically the same as the previous answer, but using the a Twig 404 template (you must create) and a one liner:

use Symfony\Component\HttpFoundation\Response;

...

return new Response($this->renderView('TwigBundle:Exception:error404.html.twig'), Response::HTTP_GONE);

Solution 3:[3]

I use this

$response = new Response();
$response->setStatusCode(410);
return $this->render('twig_template_uri', ['key'=> $value, ...], $response);

I am using this in a controller when the data exists but "isActive = false" for instance

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdulla Nilam
Solution 2
Solution 3 Ben