How to display Apache's default 404 page in PHP

前端 未结 3 868
你的背包
你的背包 2021-02-19 03:55

I have a webapp that needs to process the URI to find if a page exists in a database. I have no problem directing the URI to the app with .htaccess:

Options +Fo         


        
3条回答
  •  自闭症患者
    2021-02-19 04:43

    I don't think you can "hand it back" to Apache, but you can send the appropriate HTTP header and then explicitly include your 404 file like this:

    if (! $exists) {
        header("HTTP/1.0 404 Not Found");
        include_once("404.php");
        exit;
    }
    

    Update

    PHP 5.4 introduced the http_response_code function which makes this a little easier to remember.

    if (! $exists) {
        http_response_code(404);
        include_once("404.php");
        exit;
    }
    

提交回复
热议问题