Should I throw a 404 to a request like /photo.php?

2019-06-21 22:20发布

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?

5条回答
劳资没心,怎么记你
2楼-- · 2019-06-21 22:49

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."

查看更多
不美不萌又怎样
3楼-- · 2019-06-21 22:52

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.

查看更多
在下西门庆
4楼-- · 2019-06-21 22:55

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.

查看更多
冷血范
5楼-- · 2019-06-21 23:03

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

查看更多
一纸荒年 Trace。
6楼-- · 2019-06-21 23:06

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

Hope that helps,

查看更多
登录 后发表回答