3

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.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Response.html#method_sendHeaders – r3wt Jul 13 '15 at 09:47

4 Answers4

3

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;}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Hi, we want to implement this, did it had a long-term effect? I guess it's hard to really know. – COil Jul 20 '18 at 08:10
0

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);
COil
  • 7,201
  • 2
  • 50
  • 98
0

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

Ben
  • 1
0

You can use GoneHttpException

throw new GoneHttpException();
Gautier
  • 1,066
  • 1
  • 13
  • 24