阻止的HTTP方法未返回HTTP 405响应 [英] Blocked HTTP Methods are not returning HTTP 405 response
本文介绍了阻止的HTTP方法未返回HTTP 405响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当请求方法不是GET
或POST
时,我如何配置(理想情况下)Apache(或替代地)PHP以使用 HTTP 405 专门响应 ?
How do I configure (ideally) Apache (or alternatively) PHP to respond specifically with an HTTP 405 when the request method is not GET
or POST
?
我的Apache .htaccess
尝试会阻止请求,但会返回错误的HTTP 403 响应:
My Apache .htaccess
attempt does block requests except it returns the incorrect HTTP 403 response:
<LimitExcept GET POST>
Order Allow,Deny
Deny from all
</LimitExcept>
在PHP中,我在处理请求的绝对开始处使用了以下内容,但它完全被忽略了:
In PHP I've used the following at the absolute start of where requests are handled and it just gets completely ignored:
if (!in_array($_SERVER['REQUEST_METHOD'],array('GET','POST')))
{
header('Access-Control-Allow-Methods: GET, POST');
header('HTTP/1.1 405');
die();
}
推荐答案
您可以使用以下重定向规则禁用方法并返回405错误代码.
You can use below redirect rule to disable method and return 405 error code.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} !^(POST|GET)$
RewriteRule .* - [R=405]
这篇关于阻止的HTTP方法未返回HTTP 405响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文