Reputation: 24061
My site has a list of articles, each article is unpublished and published a number of times.
When the published flag is off (0) the article is no longer accessible.
When the article is no longer accessible I redirect the user to the homepage, I have tried using a 301 redirect:
header("Location: /", true, 301);
This causes problems when an article is published, then unpublished, and then published again, the browser seems to cache the unpublished redirect to the homepage and you can no longer view the article - even if it has it's published flag set to 1.
So I tried a 302:
header("Location: /", true, 302);
But I've been reading that the use of this is not advised.
Is there a better way to achieve this?
Upvotes: 0
Views: 75
Reputation: 31730
You're using HTTP codes inappropriately.
The redirect headers (301, et al) are intended to indicate that the requested content does exist and is reachable, it's just not at the location requested any more. If you've unpublished a document then it's not accessible from any location. Sending a 3XX redirect code in this case is wrong because you're misleading the browser and other user agents such as Google's bots.
You should be sending a code that indicates that the content is no longer available (410 Gone, 403 Forbidden or 404 Not Found) and display an error page. That error page could be a copy of the home page if you want, but I'd advise against that for usability reasons. It's better to tell people that content is no longer available than just redirect to the home page.
Incidentally, 302 no longer means "moved temporarily", it now means "found". The correct HTTP code for temporary redirects is now 303 ("See other").
Upvotes: 2