如何在ApachePHP5.0中启用PUT方法
我在htaccess中有一个内容如何在ApachePHP5.0中启用PUT方法,php,apache,.htaccess,put,Php,Apache,.htaccess,Put,我在htaccess中有一个内容 Options FollowSymLinks RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule .* index.php Rewrit
Options FollowSymLinks
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule .* index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
如何添加以启用PUT方法?
感谢约定讨论传输要使用的底层方法(在POST请求中)的
X-HTTP-METHOD-OVERRIDE
头
您可以在php应用程序中获取此请求头,以定义是否处理PUT请求。以下是可能适合您的解决方案
.htaccess
示例:
Options FollowSymLinks
RewriteEngine on
RewriteBase /path-to-target-directory
RewriteCond %{REQUEST_METHOD} !PUT
RewriteRule ^/put\.php$ - [F]
RewriteCond %{REQUEST_METHOD} PUT
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/put\.php$ - [F]
RewriteCond %{REQUEST_METHOD} PUT
RewriteRule ^(.*)$ put.php?url=$1 [L]
有关更多信息,请参阅:使用apache“启用”PUT请求不需要任何操作
如果您的意思是您没有收到PUT请求发送的数据,那么您的代码中需要一些类似以下的php代码:
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$data = file_get_contents('php://input');
}
由于PUT请求不会填充
$\u POST
(显然,因为它不是POST请求)。除了简化,在您的上下文中使用PUT over POST还有什么好处吗?启用PUT做什么?要使PUT请求与PHP一起工作,没有什么可做的,除非您的基本配置阻止它。我不知道你在问什么。.htaccess
中的重写规则
指令看起来彼此冲突。@PHPConnect-因为他可能正在创建一个RESTful web服务。因此,如果他正在创建一个资源,他将使用POST,如果他正在更新一个资源,他将使用PUT。就像您应该使用RESTful web服务一样。