If the user comes to page A and this page does not exist, then do not redirect: just send the 404 error code from page A - and, to be pleasant for your user, HTML content indicating that the page does not exist.
Thus, the browser (and this is even more true for crawlers!) Will know that the page that is not found is page A, and not something else that you would try to redirect.
The same is true for other types of errors, by the way: if a specific URL matches the error, then the error code should be sent from that URL.
Basically, quite simple:
if (page not found) { header("404 Not Found"); echo "some nice message that says the page doesn't exist"; die; }
(Well, you could deduce something nicer, of course, but you get the idea ;-))
source share