Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display default Apache 404 from PHP

I have looked around, and haven't found any questions regarding what I need.

I would like to make something like an "admin.php", but I would like to make it where it returns a 404 if the conditions aren't right. However, a 404 that looks different from all the rest of the 404 pages will hint to the attacker that "admin.php" is a real file.

I don't want to make my own custom 404 page, I think Apache's default 404 is good enough for me. How do I send a 404 that triggers apache to send the already ErrorDocument. I assume this error document, after searching for it, is located (in Ubuntu): "/usr/share/apache2/error/HTTP_NOT_FOUND.html.var"

When I use things like:

<?php
    http_response_code(404);
    die();
?>

it looks like this in the web browser:

enter image description here

When I request a URL that truly isn't on the web server it looks like this:

enter image description here

I would like to know how I could make the "/admin.php" look like the second image, using Apache's default 404 page. Is there a way to do this without using .htaccess files, just from PHP? If not, I'll just have to figure out how to mess with Apache settings then.

like image 673
Eliter Avatar asked Jun 19 '17 02:06

Eliter


People also ask

Can a 404 page be PHP?

php file, you can create one as a simple HTML file with a . php extension and then upload it to your site's theme directory. Any time a 404 error occurs, WordPress will serve up this 404. php page to the user.


1 Answers

There isn't a way to do this just by returning a 404 as Apache will find the file and therefore will no longer throw an error page. The best thing you can do is emulate the Apache error page:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?> was not found on this server.</p>
</body></html>
like image 197
sjdaws Avatar answered Sep 17 '22 18:09

sjdaws