php 使用 .htaccess RewriteRule 时,POST 值似乎丢失了。GET 值没问题。怎么修?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8826861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:35:31  来源:igfitidea点击:

Seems like POST values are lost when .htaccess RewriteRule used. GET values are OK. How to fix?

php.htaccessmod-rewritepostparameters

提问by Haradzieniec

Several days ago I had a question about removing index.php from the address bar, so the address of the page looks shorter and better. The shortest solution of this problem was (RewriteRule ^index.php / [L,R=301] in the .htaccess file). And it works!

前几天有一个问题,就是把地址栏里的index.php去掉,这样页面的地址看起来更短更好看。这个问题的最短解决方案是(RewriteRule ^index.php / [L,R=301] in the .htaccess file)。它有效!

Since I put that string into the .htaccess, some pages are redirected to the main page. I spent a lot of time to guess, why. As I understand, the answer is: with RewriteRule ^index.php / [L,R=301], $_POST parameters are not sent to the next page. $_GET parameters are OK. Once I remove RewriteRule ^index.php / [L,R=301]from .htaccess, everything becomes fine as usual. Why does it happen and how to fix that?

由于我将该字符串放入 .htaccess 中,因此一些页面被重定向到主页。我花了很多时间去猜测,为什么。据我了解,答案是: with RewriteRule ^index.php / [L,R=301], $_POST 参数不会发送到下一页。$_GET 参数没问题。一旦我RewriteRule ^index.php / [L,R=301]从 .htaccess 中删除,一切都会像往常一样好。为什么会发生这种情况以及如何解决?

Thank you.

谢谢你。

回答by mario

The [R]flag will incur a redirect. And user-agents issue a redirect as GETrequest. There is nothing that can be done if you really want to shorten URLs down to the /root path.

[R]标志将导致重定向。并且用户代理发出重定向作为GET请求。如果您真的想将 URL 缩短到/根路径,则无能为力。

You could however block POST requests specifically from being rewritten/redirected:

但是,您可以专门阻止 POST 请求被重写/重定向:

RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^index.php / [L,R=301]

回答by Gerben

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.

您可以尝试使用[L,R=307]代替。307 不能根据规范更改请求方法,但我不知道浏览器是如何实现 307 的。

But the root of the problem is the use of <form action="____/index.php" ...

但问题的根源在于使用 <form action="____/index.php" ...

Just leave the action empty to POST to the current url e.g.

只需将操作留空即可发布到当前网址,例如

回答by Johny

I'm using something like:

我正在使用类似的东西:

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(css|images|js)/

# don't rewrite existing files, directories and links

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l


# rewrite everything else to index.php

RewriteRule .* index.php [L]

</IfModule>

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?

它适用于所有请求,通过 index.php 文件重写它。如果您需要重定向 301(代表永久移动代码),请查看以下问题:是否可以重定向发布数据?

回答by Wrikken

POST values will NEVER survive an external redirect (the R=301), it's a security liability, so browsers will never support that. Remove the R=301and 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.)

POST 值永远不会在外部重定向(the R=301)中幸存下来,这是一种安全责任,因此浏览器永远不会支持它。删除它R=301,你会没事的。您只需要将页面的所有现有链接更改为更短/更漂亮的链接(<a>'s 以及表单操作等)

回答by Adrian

I had the same problems but my htacces was like this:

我有同样的问题,但我的 htacces 是这样的:

RewriteEngine on
RewriteRule .* index.php [NC]

Just change NC for L and everything works fine.

只需将 NC 更改为 L,一切正常。

Final code:

最终代码:

RewriteEngine on
RewriteRule .* index.php [L]

回答by Rao

In My case I used .htaccess. Refer : PHP $_POST not working?

就我而言,我使用了 .htaccess。参考:PHP $_POST 不工作?

i.e action="booking.php" to action="booking" worked for me

即 action="booking.php" 到 action="booking" 对我有用