Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to display 404 page in PHP? [duplicate]

So currently when a user loads a page that doesn't exist, or something goes wrong - I redirect users to a 404 page using header in PHP:

header('Location: www.website.com/404'); //This is simplified intentionally

This page displays a 404 icon with a link back to the homepage, and some other information

Recently in Google Webmaster tools I've been getting a few messages that I'm not returning a 404 page correctly.

Should I be returning http_response_code when displaying the 404 page? Or should I be handling 404 differently?


1 Answers

You can't use this. There can be only one status code. And in case of redirect it will be 301, not 404.

So you should make it

http_response_code(404);
include '404.html'; // or whatever
exit;
like image 158
Shuvo Shuvo Avatar answered Jan 27 '26 03:01

Shuvo Shuvo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!