I have a site which, for a long time, redirect the user directly to the home page if the URL was incorrect.
Instead of returning 404 error, the script used header('location: /'); die();
.
I've changed that line to header("HTTP/1.0 404 Not Found");die();
and the server started to send the 404 error message.
But, now I get the browser 404 error and not my custom 404 page. So, I've opened the .htaccess file and added
ErrorDocument 404 /index.php
But no success, I'm still getting the browser 404 page and not my 404 page (I've also tried to set it under /etc/apache2/sites-available/site-conf)
Also tried:
$_SERVER['REDIRECT_STATUS'] = 404;
header("HTTP/1.1 404 Not Found");
header('location: /');
die();
And still - no success
Any ideas?
Use:
header("HTTP/1.1 404 Not Found");
include('index.php'); // Maybe you have to adjust the path;
exit;
When you use header(location)
, you are setting the status to 3xx. And if the status is 404, the browser does not need to follow any location headers. It should follow the location header only in 3xx responses.