使用 .htaccess RewriteRule 时,POST 值似乎会丢失。 GET 值没问题。如何修复?

发布于 2024-12-26 04:08:04 字数 505 浏览 2 评论 0 原文

几天前我有一个关于从地址栏中删除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],一切都会像往常一样正常。 为什么会发生这种情况以及如何解决?

谢谢。

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!

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?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

っ〆星空下的拥抱 2025-01-02 04:08:04

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

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

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

The [R] flag will incur a redirect. And user-agents issue a redirect as GET 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:

RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^index.php / [L,R=301]
合约呢 2025-01-02 04:08:04

您可以尝试使用 [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.

|煩躁 2025-01-02 04:08:04

我正在使用类似的东西:

<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>

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

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?

不奢求什么 2025-01-02 04:08:04

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 the R=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.)

淤浪 2025-01-02 04:08:04

我遇到了同样的问题,但我的 htacces 是这样的:

RewriteEngine on
RewriteRule .* index.php [NC]

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

最终代码:

RewriteEngine on
RewriteRule .* index.php [L]

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

RewriteEngine on
RewriteRule .* index.php [NC]

Just change NC for L and everything works fine.

Final code:

RewriteEngine on
RewriteRule .* index.php [L]
执着的年纪 2025-01-02 04:08:04

就我而言,我使用了 .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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文