Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show 404 error page from PHP file, without redirecting

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.

like image 603
veeveeoor Avatar asked Nov 30 '13 13:11

veeveeoor


People also ask

How do I trigger a 404 error?

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.

Is 404 a redirect?

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.


1 Answers

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();
like image 65
aksu Avatar answered Nov 05 '22 21:11

aksu