Should I throw a 404 to a request like /photo.php?id=123 if photo #123 does not exist?

…衆ロ難τιáo~ 提交于 2020-01-14 10:23:06

问题


The script will be called from a URL like example.com/photo.php?id=123 or example.com/photos/123 depending on if the have the pretty URLs featured enabled.

If photo #123 does not exist, a request to example.com/photos/123 should throw a 404 error. But, what about example.com/photo.php?id=123?


回答1:


The relevant RFC is 2616, specifically the sections on status codes, requests, and URIs. Specifically, the query string is considered part of the URI, so a 404 is the proper response since it means:

The server has not found anything matching the Request-URI.

If you can know that a photo has been permanently deleted, you may return 410.

I would not return 200 and say "no results found."




回答2:


You should treat both ...?id=123 and .../123 URLs the same, cause they are equal - they just have a little bit different form.

And yes, you should throw an 404 - Not Found error cause given resource doesn't exists. However 404 pages shouldn't never look like:

404 Not found - you're @#$@#$@!

404 for URL like ...photo.php should contain a list of suggested resources (different photos that user might wanted to visit), some kind of search form - in other words: it should be a page that allows me to do something rather that just throw error message.




回答3:


That depends, I suppose.

If photo.php?id=123 is a page showing the photo with an id of 123, then yes, it should throw a 404. 404 means that a resource was not find when it was expected to be found - this is semantically correct.

However, on the odd chance that your semantic intent for photo.php?id=123 was for it to be a page searching for a photo with the id of 123, then it's perfectly correct to return 200 with a message saying that no results were returned.

Ultimately, it doesn't make a huge amount of difference. I'm not very well acquainted with how HTTP response codes affect the way search engines index your page, but I suspect that 404's will not get indexed in the same way. You probably don't want the page being indexed if there's nothing to display.

TL;DR I would throw 404.




回答4:


Yes, you should throw a 404 if it has never existed:

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.

But if it has existed before, you should respond a 410:

The requested resource is no longer available at the server and no forwarding address is know

From Status Code Definitions




回答5:


I think you should throw the 404. This you can easily make using a .htaccess file,

Hope that helps,



来源:https://stackoverflow.com/questions/3586876/should-i-throw-a-404-to-a-request-like-photo-phpid-123-if-photo-123-does-not

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