用PHP和.htaccess强制404不工作

时间:2022-03-28 11:02:02

I am attempting to throw my own 404 in PHP depending on certain GET vars. But the following is not working. I can confirm that the pge header is coming back with a '404 status code' though. .htaccess just doesn't seem to be redirecting correctly. Am I missing something?

我试图在PHP中抛出自己的404,具体取决于某些GET变量。但以下是行不通的。我可以确认pge标题回来时带有'404状态代码'。 .htaccess似乎没有正确重定向。我错过了什么吗?

PHP Code:

PHP代码:

if(!$_GET['page']){
    header('HTTP/1.0 404 Not Found');
}

.htaccess Code:

.htaccess代码:

ErrorDocument 404 /404.html

Many thanks!

非常感谢!

2 个解决方案

#1


12  

As far as Apache's concerned, it's done its job as it has properly found the page/script that the user's request called for. The fact that the script is outputting a 404 header is irrelevant to Apache, since its job was completed properly.

就Apache而言,它已经完成了它的工作,因为它已经正确地找到了用户请求所要求的页面/脚本。脚本输出404标头的事实与Apache无关,因为它的工作已正确完成。

You'd need to have your PHP script output the 404 header, and include the page the Apache 404 handler points at it:

您需要让PHP脚本输出404标头,并包含Apache 404处理程序指向的页面:

if (!$_GET['page']) {
    header('HTTP/1.0 404 Not Found');
    include('404.html');
    exit();
}

Don't do a redirect to the 404 page, as that'd just turn the hit into a normal GET request, with a 200 OK result.

不要重定向到404页面,因为它只是将命中转换为正常的GET请求,结果为200 OK。

#2


0  

You cannot make the error page start with a number on a XAMPP test server I know...

您不能在我知道的XAMPP测试服务器上使用数字开始错误页面...

It may be true for windows ... period.

对于windows ...期间可能是这样。

that one eluded me for hours!

那个人几个小时都没找我!

Try renaming your error page to:

尝试将您的错误页面重命名为:

error404.html

then change your .htaccess Error section for your 404 to:

然后将404的.htaccess错误部分更改为:

ErrorDocument 404 /error404.html

And don't forget to start it with the forward slash. That will fix it.

不要忘记用正斜杠开始它。这将解决它。

#1


12  

As far as Apache's concerned, it's done its job as it has properly found the page/script that the user's request called for. The fact that the script is outputting a 404 header is irrelevant to Apache, since its job was completed properly.

就Apache而言,它已经完成了它的工作,因为它已经正确地找到了用户请求所要求的页面/脚本。脚本输出404标头的事实与Apache无关,因为它的工作已正确完成。

You'd need to have your PHP script output the 404 header, and include the page the Apache 404 handler points at it:

您需要让PHP脚本输出404标头,并包含Apache 404处理程序指向的页面:

if (!$_GET['page']) {
    header('HTTP/1.0 404 Not Found');
    include('404.html');
    exit();
}

Don't do a redirect to the 404 page, as that'd just turn the hit into a normal GET request, with a 200 OK result.

不要重定向到404页面,因为它只是将命中转换为正常的GET请求,结果为200 OK。

#2


0  

You cannot make the error page start with a number on a XAMPP test server I know...

您不能在我知道的XAMPP测试服务器上使用数字开始错误页面...

It may be true for windows ... period.

对于windows ...期间可能是这样。

that one eluded me for hours!

那个人几个小时都没找我!

Try renaming your error page to:

尝试将您的错误页面重命名为:

error404.html

then change your .htaccess Error section for your 404 to:

然后将404的.htaccess错误部分更改为:

ErrorDocument 404 /error404.html

And don't forget to start it with the forward slash. That will fix it.

不要忘记用正斜杠开始它。这将解决它。