Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom 404 shows "File not found" for php files instead of redirecting

I'm trying to create a custom 404 error page.

What I have so far works very well with all unexisting documents, folders, images, htmls, etc. - opens my custom 404 page (with output Custom 404!)

However, when I try to access a file with a .php extension that does not exist I get this:

404 response

...being a "File not found." on the screen, rather than being redirected to my 404 document.

My .htaccess

RewriteEngine On
ErrorDocument 404 /404.php

My 404.php

<?php

header("HTTP/1.0 404 Not Found");
include '404.html';
die();

?>

My 404.html

<!DOCTYPE html>
<html>
<head>
<title>Custom 404 | Not found!</title>
</head>
<body>

Custom 404!

</body>
</html>

EDIT

To be more clear about what is happening here.

I believe paths and .htaccess work just fine but there has to be some trick with php. Here are few examples:

When I try to open some files that do not exist on my server:

  • myurl.com/folderthatdoesntexist/ -> opens correct 404 / echoes Custom 404!
  • myurl.com/imagethatdoesntexist.jpg -> opens correct 404 / echoes Custom 404!
  • myurl.com/htmlthatdoesntexist.html -> opens correct 404 / echoes Custom 404!
  • myurl.com/phpthatdoesntexist.php -> DOES NOT open correct 404 -> says file not found
like image 561
Biker John Avatar asked May 08 '18 01:05

Biker John


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.

What is a 404 PHP?

404 is the HTTP error number returned when a resource is unable to be located on the server. The PHP code at the top of the above file returns this code to ensure systems such as search engines don't mistake the page for real content.


1 Answers

I suspect the difference is that php files launch the interpreter while html files are just served. A solution that works but might not be the only one or the best one is to add this directive to the host configuration file:

ProxyErrorOverride on
like image 141
Ed Gadziemski Avatar answered Nov 15 '22 16:11

Ed Gadziemski