PHP 标头(“位置:/404.php”,真,404)不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6142244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:28:08  来源:igfitidea点击:

PHP header( "Location: /404.php", true, 404 ) does not work

phpredirecthttp-status-code-404

提问by Vivienne

I'd like to use the following to redirect pages that are no longer present in the database to the custom 404 page:

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

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

But this actually does not redirect, but just shows an empty page (because of the exit() call before any output to the browser).

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

I've also tried the following:

我还尝试了以下方法:

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

With a 'ErrorDocument 404 /404.php' in my .htaccess file, but this also just shows an empty page.

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

And if I do this:

如果我这样做:

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

It does redirect, but with a 302 header.

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

Any help would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by Danzan

Maybe without redirect?

也许没有重定向?

if ( !$found ):
   include('../404.php');
   exit();
endif;

回答by Jacob Christensen

I know it's a old issue but I just found it handy:

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

php set status header to 404 and add a refresh to correct page, like a 2 seconds after.

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

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

Then you have the 404 page showing up a few second before redirecting to fx search page.

然后,在重定向到外汇搜索页面之前,您会在几秒钟内显示 404 页面。

In my case, Symfony2, with a exception listener:

在我的例子中,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');

time ( 2) can be 0.2 if you wish very short time.

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

回答by dynamic

You can't have an header 404 with a Location:

你不能有一个带有位置的标题 404:

You should display the error page and set a meta refreshwith the new url if you want to redirect

meta refresh如果您想重定向,您应该显示错误页面并使用新的 url设置

回答by Ayd?n Antmen

I've tried and make sure to check error_log file that is correct way is send 404 headers and then include error page to next line.

我已经尝试并确保检查 error_log 文件的正确方式是发送 404 标头,然后将错误页面包含到下一行。

<?php
header('HTTP/1.1 404 Not Found');
include 'search.php'; // or 404.php whatever you want...
exit();
?>

Finally you have to use exit() for text based browsers.

最后,您必须将 exit() 用于基于文本的浏览器。

回答by Fosco

I think your problem is because of the output buffering you're starting, or because you can't redirect with a 404. The first code example shows the output buffer starting but exiting instead of cleaning the output buffer and stopping output buffering.

我认为您的问题是因为您正在启动输出缓冲,或者因为您无法使用 404 重定向。第一个代码示例显示了输出缓冲区开始但退出而不是清理输出缓冲区并停止输出缓冲。

Change the first example to:

将第一个示例更改为:

if (!$found) {
  ob_end_clean();
  header('Location: /404.php');
  exit();
}