PHP 和 .htaccess 强制 404 不起作用
我试图根据某些 GET 变量在 PHP 中抛出我自己的 404 。但以下不起作用。我可以确认 pge 标头返回时带有“404 状态代码”。 .htaccess 似乎没有正确重定向。 我错过了什么吗?
PHP 代码:
if(!$_GET['page']){
header('HTTP/1.0 404 Not Found');
}
.htaccess 代码:
ErrorDocument 404 /404.html
非常感谢!
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 Code:
if(!$_GET['page']){
header('HTTP/1.0 404 Not Found');
}
.htaccess Code:
ErrorDocument 404 /404.html
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就 Apache 而言,它已经完成了它的工作,因为它已经正确地找到了用户请求所调用的页面/脚本。脚本输出 404 标头这一事实与 Apache 无关,因为它的工作已正确完成。
您需要让 PHP 脚本输出 404 标头,并包含 Apache 404 处理程序指向它的页面:
不要重定向到 404 页面,因为这只会将命中转换为正常的 GET 请求,结果为 200 OK。
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.
You'd need to have your PHP script output the 404 header, and include the page the Apache 404 handler points at it:
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.
在我知道的 XAMPP 测试服务器上,您无法使错误页面以数字开头...
这对于 Windows 来说可能是正确的...期间.
我花了几个小时才发现这个!
尝试将错误页面重命名为:
然后将 404 的 .htaccess 错误部分更改为:
并且不要忘记以正斜杠开头。这样就可以解决问题了。
You cannot make the error page start with a number on a XAMPP test server I know...
It may be true for windows ... period.
that one eluded me for hours!
Try renaming your error page to:
then change your .htaccess Error section for your 404 to:
And don't forget to start it with the forward slash. That will fix it.