Apache重写规则和POST数据


Apache Rewrite Rule and POST Data

我已经成功安装了一个URL重写策略,我已经包含在下面(.htaccess):

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^rest/(['w'd~%.:_'/-]+)$ controllers/rest_api.php?params=$1&v=t [NC]

但是,它正在丢失POST数据。我看到其他问题通过将完整url更改为相对url(我的是)来解决这个问题,或者通过安装像MOD_PROXY这样的东西。然而,如果可能的话,我想保持这一点,而不需要安装额外的apache mod。我也试过[NC,L]了。

任何想法?重写功能,但我只是失去了POST数据。但是,它将POST保留为request_method。它还保留了标头,因为它通过了我正在构建的API的认证块。

您可能需要2个重写条件来停止对有效文件和目录的重写。

实际上没有特殊的设置使POST工作。在DocumentRoot中创建一个.htaccess,如下所示:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^rest/(['w'd~%.:/-]+)$ info.php?params=$1&v=t [L,QSA]

DocumentRoot中创建info.php为:

<?php phpinfo(); ?>

最后在DocumentRoot下创建一个名为form.html的表单:

<html><body>
<form method="post" action="/rest/queue/submit.xml?locations=this%20is%20a%20test">
   <input type="text" name="foo" value="foo"><br />
   <input type="text" name="bar" value="bar"><br />
   <input type="text" name="baz" value="baz"><br />
   <input type="submit" name="go" value="Submit">
</form>
</body></html>

最后在浏览器中打开localhost/form.html并单击submit。它将打开localhost/info.php,并应该显示您的POST数据完整。

我意识到这个线程有多老,但我想添加另一个潜在的原因,mod重写丢失您的POST数据。

如果您的一些url运行正常,甚至像预期的那样维护POST数据,而其他url则没有,请检查Apache错误日志。Apache可能会抛出"协商:发现匹配请求:/path/到/something的文件(没有可以协商的)"。如果是这种情况,打开Apache配置文件并关闭MultiViews选项,重新启动Apache,这可能会解决问题。本博客有更详细的解释。

https://www.bennadel.com/blog/2218-negotiation-discovered-file-s-matching-request-none-could-be-negotiated.htm