POST 在 htaccess 指令后变为 GET

编程入门 行业动态 更新时间:2024-10-16 02:29:28
本文介绍了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

本文发布于:2023-10-11 14:10:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   POST   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!