如何显示Apache默认的404页在PHP [英] How to display Apache's default 404 page in PHP

查看:393
本文介绍了如何显示Apache默认的404页在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要处理URI来查找是否存在于数据库中的网页web应用程序。我没有问题,指导URI到应用程序使用的.htaccess:

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 +FollowSymlinks
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [NC]

我的问题是,如果页面不存在,我不希望使用自定义的404处理器用PHP写的,我想确实显示了默认的Apache 404页。有没有什么办法让PHP手执行回阿帕奇当确定该网页不存在?

My problem is that if the page does not exist, I do not want to use a custom 404 handler written in PHP, I would like do show the default Apache 404 page. Is there any way to get PHP to hand execution back to Apache when it has determined that the page does not exist?

推荐答案

我知道对于上述方案是有这种类型的PHP code在指数的唯一可能途径。 PHP

The only possible way I am aware of for the above scenario is to have this type of php code in your index.php:

<?php
if (pageNotInDatabase) {
   header('Location: ' . $_SERVER["REQUEST_URI"] . '?notFound=1');
   exit;
}

然后稍微修改你的.htaccess这样的:

And then slightly modify your .htaccess like this:

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 code和与阴性检查增加在.htaccess页面同样也不会被转发到index.php的下一次。

That way Apache will show default 404 page for this special case because of extra query parameter ?notFound=1 added from php code and with the negative check for the same in .htaccess page it will not be forwarded to index.php next time.

PS:的URI像 /富,如果在数据库中找不到将成为 /富NOTFOUND = 1 在浏览器中。

PS: A URI like /foo, if not found in database will become /foo?notFound=1 in the browser.

这篇关于如何显示Apache默认的404页在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆