使用 .htaccess RewriteRule 时,POST 值似乎会丢失。 GET 值没问题。如何修复?
几天前我有一个关于从地址栏中删除index.php的问题,这样页面的地址看起来更短更好。这个问题的最短解决方案是( RewriteRule ^index.php / .htaccess 文件中的 [L,R=301])。它有效!
由于我将该字符串放入 .htaccess 中,因此某些页面被重定向到主页。我花了很多时间去猜测,为什么。据我了解,答案是:使用 RewriteRule ^index.php / [L,R=301]
,$_POST 参数不会发送到下一页。 $_GET 参数没问题。
一旦我从 .htaccess 中删除 RewriteRule ^index.php / [L,R=301]
,一切都会像往常一样正常。
为什么会发生这种情况以及如何解决?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
[R]
标志将导致重定向。用户代理发出重定向作为GET
请求。如果您确实想将 URL 缩短到/
根路径,则无能为力。但是,您可以专门阻止 POST 请求被重写/重定向:
The
[R]
flag will incur a redirect. And user-agents issue a redirect asGET
request. There is nothing that can be done if you really want to shorten URLs down to the/
root path.You could however block POST requests specifically from being rewritten/redirected:
您可以尝试使用
[L,R=307]
代替。 307不能根据规范改变请求方法,但我不知道浏览器是如何实现307的。但问题的根源是使用
只需将操作留空即可 POST 到当前 url,例如
You could try using
[L,R=307]
instead. 307's must not change the request-method according to the spec, but I don't know how browser implemented 307.But the root of the problem is the use of
<form action="____/index.php" ...
Just leave the action empty to POST to the current url e.g.
我正在使用类似的东西:
它适用于所有请求,通过index.php 文件重写它。
如果您需要重定向 301(代表永久移动代码),请查看以下问题:是否可以重定向发布数据?
I'm using something like:
And its working for all requests, rewriting it via index.php file.
If you need to redirect 301 (which stands for Moved Permanently code) check out this question: Is it possible to redirect post data?
POST 值永远不会在外部重定向(
R=301
)后继续存在,这是一种安全责任,因此浏览器永远不会支持这一点。删除R=301
就可以了。您只需将页面的所有现有链接更改为更短/更漂亮的链接(的,但也包括表单操作等)
POST values will NEVER survive an external redirect (the
R=301
), it's a security liability, so browsers will never support that. Remove theR=301
and you will be fine. You just should alter all existing links to the page to the shorter/prettier one (<a>
's but also form actions etc.)我遇到了同样的问题,但我的 htacces 是这样的:
只需将 NC 更改为 L,一切正常。
最终代码:
I had the same problems but my htacces was like this:
Just change NC for L and everything works fine.
Final code:
就我而言,我使用了 .htaccess。
请参阅:PHP $_POST 不工作?
即
action="booking.php" 到 action="booking" 对我有用
In My case I used .htaccess.
Refer : PHP $_POST not working?
i.e
action="booking.php" to action="booking" worked for me