gpt4 book ai didi

PHP header ( "Location:/404.php", true, 404 )不起作用

转载 作者:行者123 更新时间:2023-12-03 00:04:38 24 4
gpt4 key购买 nike

我想使用以下命令将数据库中不再存在的页面重定向到自定义 404 页面:

ob_start();
....
if ( !$found ):
header( "Location: /404.php", true, 404 );
exit();
endif;

但这实际上并没有重定向,而只是显示一个空页面(因为在向浏览器输出任何内容之前调用了 exit() )。

我还尝试了以下方法:

if ( !$found ):
header( "HTTP/1.1 404 Not Found" );
exit();
endif;

我的 .htaccess 文件中有一个“ErrorDocument 404/404.php”,但这也只显示一个空页面。

如果我这样做:

if ( !$found ):
header( "HTTP/1.1 404 Not Found" );
header( "Location: /404.php" );
exit();
endif;

它确实重定向,但带有 302 header 。

任何帮助将不胜感激。

最佳答案

我知道这是一个老问题,但我发现它很方便:

php 将状态 header 设置为 404 并添加刷新以正确的页面,例如 2 秒后。

header('HTTP/1.1 404 Not Found');
header("Refresh:0; url=search.php");

然后,404 页面会显示几秒钟,然后重定向到 fx 搜索页面。

就我而言,Symfony2,带有异常监听器:

$request  = $event->getRequest();
$hostname = $request->getSchemeAndHttpHost();
$response->setStatusCode(404);
$response->setContent('<html>404, page not found</html>');
$response->headers->set('Refresh', '2;url='.$hostname.'/#!/404');

如果您希望时间很短,时间 (2) 可以是 0.2。

关于PHP header ( "Location:/404.php", true, 404 )不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6142244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com