PHP标头("Location:/404.php",true,404)不起作用 [英] PHP header( "Location: /404.php", true, 404 ) does not work

查看:172
本文介绍了PHP标头("Location:/404.php",true,404)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

但这实际上并不会重定向,而只会显示一个空白页面(由于在向浏览器输出任何内容之前调用exit().)

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

我也尝试了以下方法:

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

.htaccess文件中带有'ErrorDocument 404/404.php',但这也只显示一个空白页面.

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

如果我这样做:

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

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

It does redirect, but with a 302 header.

任何帮助将不胜感激.

推荐答案

也许没有重定向?

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

这篇关于PHP标头("Location:/404.php",true,404)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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