Googlebot receiving missing template error for an existing template

不羁岁月 提交于 2019-12-02 19:09:50
rchampourlier

These errors are coming from the way GoogleBot formats its HTTP_ACCEPT header. While valid (see W3 reference), it adds a q=0.6 (last figure may change) which is used as a separator. Since there is no other media type specified, this q=0.6 is not necessary and I assume this is why Rails doesn't treat the header correctly.

(It seems to depend on Rails version. On Rails 3.0.12, it raises a MissingTemplate exception.)

Adding the following code from a previous answer to the concerned controller is not sufficient: it responds with an error 406.

respond_to do |format|
  format.html
end

To make this work under Rails 3.0.12 and have something returned to the GoogleBot (better than a 406 error), you need to add this code which sets the request's format to html as soon a */*;q=0.6-like HTTP_ACCEPT is detected (Rails load the header value into request.format).

# If the request 'HTTP_ACCEPT' header indicates a '*/*;q=0.6' format,
# we set the format to :html.
# This is necessary for GoogleBot which perform its requests with '*/*;q=0.6'
# or similar HTTP_ACCEPT headers.
if request.format.to_s =~ %r%\*\/\*%
  request.format = :html
end

respond_to do |format|
  format.html
end

While working, this solution needs the code to be added to any controller action you want to be indexed by the GoogleBot, what is really not DRY!

To fix this issue once for all, I implemented a small Rack middleware which does even better: it checks the request's HTTP_ACCEPT header, and will replace any header matching */*;q=0.6 (the figures can vary) by the common */*. This is even better because since the q=0.6 has no meaning if it is not followed by another media type, this change of the header doesn't change its meaning. We don't force Rails into any given format, we just tell it any will do in a way it can understand.

You can find the middleware, the loading initializer and an integration test in this gist.

Gem version here: https://github.com/ouvrages/rails_fix_google_bot_accept

I am also getting the same, I did some investigation and came to the conclusion it is a 'bug' in Rails. */*;q=0.9 is the value of the HTTP accept parameter. I'm not exactly sure what is going on, but in Rails 3.0 this works. In Rails 3.1 it returns a 500 response, and in Rails 3.2 it returns a 406 response.

Update:

There is an open bug regarding this issue. One workaround is to set this new option in Rails 3.1:

config.action_dispatch.ignore_accept_header = true

However... if you serve any pages other than HTML you'll need to rely on the extension to denote the type (e.g. /users/1.json) instead of accept headers.

The solution to the problem is to specify the format in your action.

Up until now, I had simply had the following in my index action

def index

end

Once I inserted a respond_to block

def index
  respond_to do |format|
    format.html
  end
end

I stopped getting the missing template errors.

the interesting part in the error that you posted is :formats=>["*/*;q=0.9"]

the rails-app tries to find a template for the format "*/*;q=0.9" which is not going to work.

i guess that google is somehow using this as a format query parameter like welcome?format=*/*;q=0.9

afaik latest rails versions will just render a 406 in those cases.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!