什么导致 HTTP 405“无效方法(HTTP 动词)"?在 IIS 上将表单发布到 PHP 时出错?
我在 PHP (5.2.9-1) 应用程序中有一个表单,导致 IIS (Microsoft-IIS/6.0) 在发布时抛出以下错误:
I have one form in a PHP (5.2.9-1) application that causes IIS (Microsoft-IIS/6.0) to throw the following error when POSTed:
无法显示您要查找的页面,因为尝试访问时使用了无效方法(HTTP 动词).
The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access.
这是一个 HTTP 405 状态代码.应用程序中的所有其他表单都可以工作,因此我相信 PHP 页面的 IIS 'verbs' 设置是正确的.
It's an HTTP 405 status code. All other forms in the application work, so I believe that the IIS 'verbs' setting for PHP pages is correct.
这是客户的服务器,我无法访问它来验证设置或测试代码.我所能做的就是发送客户更换文件.IIS 服务器上的其他客户没有这样的问题.
This is a customer's server, which I have no access to for verifying settings or testing code. All I can do is send the customer replacement files. Other customers on IIS servers have no such issue.
表格非常简单:
<form method="post" action="index.php">
... fields ...
</form>
什么会导致 IIS 仅在一种形式上抛出该错误,而在其他形式上却能正常工作?
What can cause IIS to throw that error on one form only, but work fine on others?
推荐答案
我设法通过 FTP 访问客户的服务器,因此能够找到问题所在.
I managed to get FTP access to the customer's server and so was able to track down the problem.
提交表单后,我对用户进行身份验证,然后重定向到应用程序的主要部分.
After the form is POSTed, I authenticate the user and then redirect to the main part of the app.
Util::redirect('/apps/content');
错误不是发生在表单的发布上,而是发生在紧随其后的重定向上.出于某种原因,IIS 继续假定重定向的 POST 方法,然后反对 POST 到 /apps/content
因为它是一个目录.
The error was occurring not on the posting of the form, but on the redirect immediately following it. For some reason, IIS was continuing to presume the POST method for the redirect, and then objecting to the POST to /apps/content
as it's a directory.
错误消息从未表明是下一页产生了错误 - 感谢 Microsoft!
The error message never indicated that it was the following page that was generating the error - thanks Microsoft!
解决方案是添加一个尾部斜杠:
The solution was to add a trailing slash:
Util::redirect('/apps/content/');
然后 IIS 可以将重定向解析为默认文档,因为不再尝试 POST 到目录.
IIS could then resolve the redirect to a default document as is no longer attempting to POST to a directory.
相关文章