I have a file secret.php
which is included in index.php
and I want to show a 404 error page when someone tries to access secret.php
directly. But
header("Location: this_site_certainly_does_not_exist");
in secure.php
is not a solution, because I want the URL the user typed (e.g. example.com/secret.php
) to be visible in the browser's URL bar when the error page is shown, instead of redirecting.
One typical trigger for an error 404 message is when the page has been deleted from the website. The page was moved to another URL and the redirection was done incorrectly. You entered an incorrect URL address. Although it happens very rarely, sometimes the server malfunctions.
What is a 404 Redirect? 404 redirects are server response code informing a user that the web page he or she is looking for cannot be found; either due to user error when typing the url, or the web page he or she is looking for is not an actual web page.
You can do it with headers.
header("HTTP/1.0 404 Not Found");
then you can add your 404 page using for example readfile
method.
header("HTTP/1.0 404 Not Found");
echo "<h1>404 Not Found</h1>";
echo "The page that you have requested could not be found.";
exit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With