【发布时间】:2014-08-06 11:20:21
【问题描述】:
我是PHP新手,希望你能帮我解决这个问题。 如何在 PHP 中创建自定义 404 错误页面而不去任何地方或将其重定向到其他页面。
例如。 mysite.com/no-where
页面no-where 不存在。我想在该 URL 上显示带有设计的 404 错误消息。
如何获取当前网址?我对此有误。因为我想检查标题是否为 404。
$url = $_SERVER['REQUEST_URI'];
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")) {
echo 'url exists';
} else {
echo 'url does not exist';
}
【问题讨论】:
-
创建一个
404.php页面或404.htm文件,使用任何你想要的样式,然后在.htaccess中做ErrorDocument 404 /404.php,不是吗?还是认为这太“Old Skool”了? -
不,我不想将用户重定向到其他页面。我想在他/她当前正在浏览的网址上准确显示错误消息。
-
好的,那就看看韦恩的回答吧。
-
ErrorDocument 404 /404.php不会更改您的原始网址。它将保留在浏览器中的mysite.com/no-where。 -
当我尝试它时出现错误
The requested URL /no-where was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
标签: php .htaccess http-status-code-404