core icon indicating copy to clipboard operation
core copied to clipboard

Custom 404 page return 200

Open lk-KEVIN opened this issue 3 years ago • 2 comments

Hi, I was trying to use a custom html file to replace the default error page, but the body returns 200 OK instead of 404 Not Found. Tried the code for issue #343 and everything was fine, return 404

Flight::route('/status', function() {
    Flight::response()->status(404);
    echo 'The status is 404.';
});

then I try to Override using the example from the docs is when return 200 OK

Flight::map('notFound', function(){
    // Display custom 404 page
    include 'errors/404.html';
});

What should I do? neither http_response_code(404);, header("HTTP/1.1 404 Not Found"); or Apache ErrorDocument 404 errors/404.html return 404 Not Found

My only solution is replace the original flight\Engine.php error message

lk-KEVIN avatar Nov 16 '21 17:11 lk-KEVIN

Hi, this works for me:

Flight::map('notFound', function(){
	Flight::response(false)
            ->status(404)
            ->write(
                file_get_contents(__DIR__ . '/lib/views/404.htm')
            )
            ->send();
});

severak avatar Dec 30 '21 14:12 severak

You may also use:

Flight::stop(404);

sah-comp avatar Dec 30 '21 14:12 sah-comp

Or you can use Flight::halt(404, 'Not found');

@n0nag0n also can be closed.

krmu avatar Jan 03 '24 21:01 krmu

Thanks @krmu

n0nag0n avatar Jan 03 '24 21:01 n0nag0n