If record is not found in the database, should I return a 404?

一个人想着一个人 提交于 2020-02-20 09:25:28

问题


I have pages with structure like this some_page?id=123

Sometimes records are deleted, but those pages still get visited by people and search engines. One problem is that I have a php script and no MVC structure, so I have to query the DB all on the same page before I find out that the user record does not exist.

Should I return a 404 in that case? Or what is best practice?

Thanks!


回答1:


If the client (users, search engines, etc.) requests a resource that does not exist and has never existed, a 404 status ("Not Found") should be returned.

However, if a resource is requested that once existed and has since been permanently removed, a 410 status ("Gone") should be returned. A 410 status specifically states that the resource will never be available in the future, and triggers search engines to remove that page from their indexes.

A 301 status is used if a resource's location has changed permanently.




回答2:


A 404 is when something is not found and since the resource being looked up is not found, it seems entirely appropriate to me.




回答3:


That depends if you want to keep the user on your site. If you redirect permanently to another page, then make sure you also have a 301 in the headers so that search engines don't penalize you for to many links pointing to the same page.

A 404 is ok, but you will get bounces from that page if users' browsers don't render any content.

I personally think the best approach is show a friendly page for users and set headers correctly for search engines.

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes




回答4:


yes. that would be proper provided you have no substitute to capitalize with. it would be better for you to server up a similarly subjected resource or even a re-direct.




回答5:


Yes - but don't forget to offer the visitor some additional related content. Just because the url isn't found doesn't mean that the visitor should be ignored. Show a custom 404 page with some other sections of the site that may pertain - possibly the most visited pages,...



来源:https://stackoverflow.com/questions/8650898/if-record-is-not-found-in-the-database-should-i-return-a-404

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