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

查看:159
本文介绍了htaccess指令后POST变为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.

要解决此问题,请按以下步骤操作:

To fix have it this way:

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.

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

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