Steffen Brueckner
Steffen Brueckner

Reputation: 209

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.

Upvotes: 3

Views: 2600

Answers (4)

Gautier
Gautier

Reputation: 1144

You can use GoneHttpException

throw new GoneHttpException();

Upvotes: 0

Ben
Ben

Reputation: 1

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

Upvotes: 0

COil
COil

Reputation: 7606

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);

Upvotes: 0

Steffen Brueckner
Steffen Brueckner

Reputation: 209

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;}

Upvotes: 3

Related Questions