PHP:无法更改404状态码,但header()中没有错误 [英] PHP: Can't change 404 status code, but there is no error from header()

查看:356
本文介绍了PHP:无法更改404状态码,但header()中没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义的PHP网站上处理不可破坏的页面。为此,我使用这一行创建了一个.htaccess文件:

  ErrorDocument 404 /index.php 

code>

在index.php的开头,我放置了这段代码:

  header($ _ SERVER ['SERVER_PROTOCOL']。'200 OK',true,200); 

这适用于我的WAMP环境。我正在检查http标题,并且它会抛出:

  Status = OK  -  200 

但是在我的Linux主机中,我总是得到:

 状态=未找到 -  404 

奇怪的部分是PHP不会抛出任何错误总之......这就像是重写我的头文件。

我需要更改状态码头,否则IE7和IE8不会处理我的结果页面。其他浏览器可以解决这个问题。



也许我需要.htaccess或php.ini中的内容,但是没有找到任何相关的内容。或者,如果您知道任何其他方法来重定向404并返回200,请告诉我。

解决方案

更好的重写方法不存在的页面到PHP文件是这样的:

  RewriteEngine On 
RewriteCond%{SCRIPT_FILENAME}!-d
RewriteCond%{SCRIPT_FILENAME}!-f
RewriteRule(。*)index.php

这使用mod_rewrite将任何不是目录而不是文件的内容更改为PHP脚本。只要记住,如果你的PHP脚本收到一个不存在的东西的请求,它应该发送一个404头。

I'm processing unavaliable pages in my custom PHP website. To do this I created a .htaccess file with this single line:

ErrorDocument 404 /index.php

At the beginning of index.php I placed this code:

header($_SERVER['SERVER_PROTOCOL'].' 200 OK', true, 200);

This works perfect in my WAMP environment. I'm checking http headers, and it throws:

Status=OK - 200

But in my Linux hosting I always get:

Status=Not Found - 404

And the weird part is that PHP isn't throwing any error at all... it's like something is overriding my headers.

I need to change the status code header, otherwise IE7 and IE8 won't process my result page. Other browsers can deal with this.

Maybe I need something in .htaccess or php.ini, but haven't found anything related. Or, if you know any other method to redirect a 404 and return 200, let me know.

解决方案

A better method of rewriting non existent pages to a PHP file is this:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) index.php

This uses mod_rewrite to change anything that is not a directory and not a file to your PHP script. Just remember that if your PHP script recieves a request for something that isn't there, it should send a 404 header.

这篇关于PHP:无法更改404状态码,但header()中没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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