开发者

should I send status as 404 or 200?

开发者 https://www.devze.com 2022-12-19 13:32 出处:网络
I am using Rails to build a web application. In case a record is not found for a url say http://example.com/city/non_existant_city then I have following code.

I am using Rails to build a web application. In case a record is not found for a url say http://example.com/city/non_existant_city then I have following code.

 render :text => "record was not found", :status => :not_found

In firefox I see the message.

In chrome (on mac) I see a chrome 404 page and this page does not display the message I am sending.

This makes me wonder should I send status as 200. I was sending 404 so that web crawlers can underst开发者_JS百科and that this link is consistently returning 404 so after a while give up on that link.


You should use 404. Not only do crawlers stop visiting invalid links, but you are not penalized for duplicate content if it's a 404 page, as you would if it was a 200.

As for Chrome, it only replaces 404 pages if the contents are too short (less than ~520 chars)

See: http://perso.hirlimann.net/~ludo/blog/archives/2008/09/chrome-and-404s.html


If you're telling the end user that the page doesn't exist, then send a 404.

If you're telling the end user that the page exists, but the record that they were looking for does not, then create a normal page (200) and display an error message in it.

A lot of browsers display "friendly" 404 pages, which is why you aren't seeing your message.


Is it common for a record not to be found? Eg Do humans enter the city name and then, if the name is not found you could give them alternatives? If so, then I'd return a 200 page that includes a new query box, etc.

If this is a web service, then 404 is ok if all you want your api to return in such cases is "not found." But if you want your api to additionally return other information, then 200 (plus a non-success error code and the other information) is the way to go.

0

精彩评论

暂无评论...
验证码 换一张
取 消