apache header("location") 导致 [500] 内部服务器错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2354026/
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
header("location") causes [500] internal server error?
提问by animuson
I cannot figure out what could be causing this error. My Apache log is not recording any errors in the access log or error log regarding the page, yet somehow whenever I uncomment the header()line I get a 500 Internal Server Error. It can't be coming from PHP's fatal error when content is outputted before header()is called, that wouldn't cause a 500 would it?
我无法弄清楚是什么导致了这个错误。我的 Apache 日志没有在访问日志或关于页面的错误日志中记录任何错误,但不知何故,每当我取消注释该header()行时,我都会收到 500 内部服务器错误。在header()调用之前输出内容时,它不可能来自PHP的致命错误,这不会导致500吗?
header("Location: /offices/page-".ceil($cache->size() / 15));
I tested $cache->size()and it's returning 22, so it should append a 2 to the end of the string and redirect. I did have ob_start()called before the header()but I tried calling ob_end_clean()right before it and it still did nothing...
我测试过$cache->size()它返回 22,所以它应该在字符串的末尾附加一个 2 并重定向。我确实在它之前打过电话ob_start(),header()但我尝试ob_end_clean()在它之前打电话,但它仍然什么也没做......
I even tried putting header("Location: /offices");at the very beginning of the file and it still gives me a 500.
我什至尝试将其放在header("Location: /offices");文件的最开头,但它仍然给了我 500。
回答by deceze
Try to use a fully formed URL:
尝试使用完整格式的 URL:
header("Location: http://{$_SERVER['HTTP_HOST']}/offices/page-".ceil($cache->size() / 15));
回答by jasonbar
The script continues to execute after your header() call. You need put exit;immediately after it. This shouldn't cause a 500 error, though.
脚本在您的 header() 调用后继续执行。你需要exit;立即放在它之后。不过,这不应该导致 500 错误。
Edit: Evidently this worked - meaning your problem is probably in some related code further down the page?
编辑:显然这有效 - 意味着您的问题可能出在页面下方的一些相关代码中?
回答by T.Todua
Another reason might be - you need to remove spaceafter location:
另一个原因可能是-你需要删除空间后location:
header("location :header("location:
header("location :header("location:
If that's not your problem, then try to check the php & apache error logs.
如果这不是您的问题,请尝试检查 php 和 apache错误日志。
回答by RaininDown
I had the same thing going on. After repeatedly banging my head on the monitor, it isn't the header("Location: myPage.php"); line that is broken - it is myPage.php that is broken! Try redirecting to another page. I hope I save someone else time
我有同样的事情发生。在我的头反复敲打显示器后,它不是 header("Location: myPage.php"); 断线 - 断线的是 myPage.php!尝试重定向到另一个页面。我希望我能节省别人的时间

