Blocked HTTP Methods are not returning HTTP 405 response

℡╲_俬逩灬. 提交于 2020-07-10 08:58:08

问题


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?

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>

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();
}

回答1:


You can use below redirect rule to disable method and return 405 error code.

RewriteEngine On
RewriteCond %{REQUEST_METHOD} !^(POST|GET)$
RewriteRule .* - [R=405]


来源:https://stackoverflow.com/questions/62652001/blocked-http-methods-are-not-returning-http-405-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!