douzhang3822 2009-09-04 19:29
浏览 96
已采纳

如何在PHP中创建错误404?

My .htaccess redirects all requests to /word_here to /page.php?name=word_here. The PHP script then checks if the requested page is in its array of pages.

If not, how can I simulate an error 404? I tried this, but it didn't result in my 404 page configured via ErrorDocument in the .htaccess showing up.

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");

Am I right in thinking that it's wrong to redirect to my error 404 page?

  • 写回答

8条回答 默认 最新

  • dprfe04886 2017-01-11 14:28
    关注

    The up-to-date answer (as of PHP 5.4 or newer) for generating 404 pages is to use http_response_code:

    <?php
    http_response_code(404);
    include('my_404.php'); // provide your own HTML for the error page
    die();
    

    die() is not strictly necessary, but it makes sure that you don't continue the normal execution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效