PHP header 404 not working

后端 未结 4 1295
孤独总比滥情好
孤独总比滥情好 2021-01-13 01:52

Why is this not working, as in the pre-set 404 page is not loaded:

header(\"HTTP/1.0 404 Not Found\");
exit;

.htaccess has the

4条回答
  •  感动是毒
    2021-01-13 02:07

    I noticed that it does not work so I created a class with a static method to solve the problem

    I have a directory /error-pages where I store all error file

    I have a class file error.php which can be included at the top of every page

    class ServerError{
        public static function show($page){
            require $_SERVER['DOCUMENT_ROOT'].'/error-pages/'.$page.'.php';
            exit();
        }
    }
    

    So on your home.php page for example you say

    if (my404IsTrue) {
        ServerError::show("404");
    }
    

    this way. you can use it for any other kind of error be it 403 or a custom error

    Hope it Helps

提交回复
热议问题