POST 在 htaccess 指令后变为 GET [英] POST becomes GET after htaccess directives

查看:19
本文介绍了POST 在 htaccess 指令后变为 GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Apache htaccess 文件中使用以下指令隐藏 php 文件扩展名

I am hiding the php file extention using the below directives in Apache htaccess file

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

我有一个像这样的简单 html 表单

I have a simple html form like this

<form action="register.php" method="post">
  <input type="text" name="name">
  <input type="submit">
</form>

在我的 PHP 页面中,这是我之前检查表单是否已发布的方式

And in my PHP page this is how I was earlier checking whether the form has been posted

if ($_SERVER["REQUEST_METHOD"] == "POST") {
/* code to process the post */
}

但是因为我添加了 htaccess 指令来隐藏 php 文件扩展名,所以POST"变成了GET"并且上面的代码永远不会执行.有没有好的办法来处理这个问题?我知道是内部重定向导致了这种情况,但是有没有一种优雅的方法来处理这个问题?出于显而易见的原因,我不想使用以下方法

But since I added the htaccess directives to hide the php filename extensionthe "POST" becomes "GET" and the above code never executes. Is there a good way to handle this? I know it is the internal redirect that causes this but is there an elegant way to handle this? I don't want to use the below method for obvious reasons

if (isset($_POST['submit'])) {

推荐答案

POST 没有变成 GET,这是由于你的第一个重定向规则:

POST doesn't become GET, it is due to your first redirect rule:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,L,NC]

它正在捕获一个包含 .php 的请求并重定向到一个没有 .php 的 URL.您正在查看 $_SERVER["REQUEST_METHOD"] 在它被此规则重定向后.

Which is catching a request with .php in it and redirecting to a URL without .php in it. You're looking at $_SERVER["REQUEST_METHOD"] after it has been redirected by this rule.

以这种方式修复:

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,L]

即不要重定向 POST 请求以避免 POST 数据丢失.

i.e. don't redirect POST requests to avoid POST data getting lost.

这篇关于POST 在 htaccess 指令后变为 GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆