dsk61780 2013-04-05 22:39
浏览 80
已采纳

在PHP中发送404标头后,从htaccess显示404文档

In PHP I use the following code to indicate page not found:

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

In .htaccess file I have the following line to handle 404 custom document:

ErrorDocument 404 /404.html

Now when I send the PHP 404 header I expect it to show the custom 404.html defined in .htaccess but instead it shows a message "this link appears to be broken". So it doesn't show the custom 404 page and if I remove the line from .htaccess it still won't display the regular 404 page of the server and shows also the broken link message.

So how to make it show the 404 page when I send the header in PHP?

  • 写回答

2条回答 默认 最新

  • dongposhi8677 2013-04-06 08:05
    关注

    The problem here is that the custom error document denoted by ErrorDocument isn't processed unless the URL processing pipeline can't find a file (or a handler) that the URL points to. In you case, the processing pipeline does find a file, your php file, thus it never reaches a 404 on its own so the ErrorDocument is never applied.

    When apache uses the php handler to run your php file, and your php file decides to return a 404, you not only need to provide the 404 response code, but the HTML error document as well. You don't see the default apache error document because apache never thinks the request is a 404 (it handed the request off to your php file). So you need to do something like this:

    header("HTTP/1.0 404 Not Found");
    include('404.html');
    exit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据