views:

91

answers:

4

On the book Agile Web development with Rails, it is proposed that when someone tries to access some data in your web site and the record doesn't exist anymore, that the user should be redirected to a working page and display a message.

A user would go to /book/1, but a book with id 1 doesn't exist anymore, so it is redirected to /books and shown a message "That book doesn't exist". It seems to be a good user experience but to break the HTTP protocol. Should it be a temporary redirect? if so a web crawler will keep hitting that page. Should it be a permanent redirection? If so the previous content should be available there, and it isn't.

I think that a record-not-found page should issue a 404. Am I wrong? Hitting /book/1 where 1 doesn't exist anymore would return a 404 with the HTML showing exactly the same thing as /books, and maybe an error message.

Agile Web development with Rails is against that option because the user might keep hitting /book/1 generating 404s only to see what can be seen in /books.

What do you think?

A: 

If there's no 404 , search engines have no way to discover that the object has been deleted. So I suppose it's a must.

Leonid Shevtsov
Yes, that's my position, the book recommends something different.
J. Pablo Fernández
A: 

I think there's a good compromise where you render a 404 template (complete with 404 status code) that prompts the user to continue to /books or /whatever.

semanticart
A: 

if the record doesn't exist anymore, than you should probably use a 301 status code, "permanent redirect". The difference between 301 and 404, is that a 404 error code should be used in cases when the resource never existed and 301 when the resource existed, but moved.

Elad Meidar
Well, if someone asks to read comment with id 1234 and it's not in the database, I can't know whether it existed or not and certainly if I redirect to the list of comments is not because the resource is now there, but because it was the best I could provide.
J. Pablo Fernández
well, in your case you might not care, but in cases this method is used in an API, the header can provide a good explanation of what actually happened to the calling side.
Elad Meidar
+1  A: 

If the resource does not exist, send the 404 status code. It’s really that simple. Redirecting means that only the URL is (temporarily) not valid but the resource does exist.

Gumbo