内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/5953096/how-to-display-apaches-default-404-page-in-php
我有一个需要处理URI的webapp,以查找数据库中是否存在一个页面.使用.htaccess将URI指向应用程序时没有问题:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$index.php?p=$1 [NC]
我的问题是,如果页面不存在,我不想使用用 PHP 编写的自定义404处理程序,我想要显示默认的Apache 404页面.当确定该页面不存在时,是否有任何方式让PHP手动执行回Apache?
对于上述情况,我知道的唯一可能的方法是在index.php中具有这种类型的php代码:
<?php
if (pageNotInDatabase) {
header('Location: ' . $_SERVER["REQUEST_URI"] . '?notFound=1');
exit;
}
然后稍微修改你的.htaccess像这样:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{QUERY_STRING} !notFound=1 [NC]
RewriteRule ^(.*)$index.php?p=$1 [NC,L,QSA]
这样Apache将会为这个特殊情况显示默认的404页面,因为额外的查询参数?notFound = 1,从php代码中添加,而在.htaccess页面中的负数检查与下一次不会转发到index.php.
PS:像/ foo这样的URI,如果在数据库中找不到,浏览器中将会变成/ foo?notFound = 1.
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/5953096/how-to-display-apaches-default-404-page-in-php
以上所述就是小编给大家介绍的《如何在PHP中显示Apache的默认404页面》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- springboot 默认日志配置
- HDFS副本设置——默认3
- WPF 绑定的默认模式
- StackOverflow转向默认使用HTTPS
- Spring解密 - 默认标签的解析
- Java 8 中的默认方法
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Automate This
Christopher Steiner / Portfolio / 2013-8-9 / USD 25.95
"The rousing story of the last gasp of human agency and how today's best and brightest minds are endeavoring to put an end to it." It used to be that to diagnose an illness, interpret legal docume......一起来看看 《Automate This》 这本书的介绍吧!