php 是否可以重定向帖子数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/358263/
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-24 22:29:19  来源:igfitidea点击:

Is it possible to redirect post data?

phpapache.htaccess

提问by alex

I have a website where all requests are redirected silently (via .htaccess) to index.phpand then PHP is used to show the correct page (by parsing the REQUEST_URI).

我有一个网站,所有请求都以静默方式(通过.htaccess)重定向到index.php,然后使用 PHP 显示正确的页面(通过解析REQUEST_URI)。

I was wondering if it's possible to submit POST data to a fake address too?

我想知道是否也可以将 POST 数据提交到假地址?

I've currently got my form like so...

我目前的表格是这样的......

<form action="/send-mail" method="post">

And my .htaccessrule is...

而我的.htaccess规则是...

# redirect mail posting to index
RewriteRule send-mail index.php?send-mail [NC,L] 

My index.phpchecks isset($_GET['send-mail'])which works fine.

我的index.php支票isset($_GET['send-mail'])工作正常。

This however seems to drop off all the POST data that should be sent to it.

然而,这似乎放弃了应该发送给它的所有 POST 数据。

Is there a way to keep the post data? I don't want to use GET because it can't send as much information, though it might not be an issue with a simple inquiry form.

有没有办法保留帖子数据?我不想使用 GET,因为它不能发送尽可能多的信息,尽管它可能不是一个简单的查询表单的问题。

Here is my .htaccessfor redirecting to index.php

这是我.htaccess的重定向到index.php

# serve files and dirs if they exist please, otherwise send to index
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php

回答by Tautologistics

Try this:

尝试这个:

# redirect mail posting to index
     RewriteRule send-mail index.php?send-mail [NC,P]

"P" acts like "L" in that it stops processing rules but it also tells the module that the request should be passed off to the proxy module intact (meaning POST data is preserved).

“P”的作用类似于“L”,因为它停止处理规则,但它也告诉模块应该将请求原封不动地传递给代理模块(意味着 POST 数据被保留)。

回答by Bill Karwin

You should be able to simply redirect to index.php, and then in that script, access $_SERVER['REQUEST_URI']to see the original request, with "send-mail" intact.

您应该能够简单地重定向到index.php,然后在该脚本中访问$_SERVER['REQUEST_URI']以查看原始请求,“发送邮件”完好无损。

By the way, "can't send as much information" is not the reason to use POST. The reason to use POST is that the request will modify data on your site, instead of simply retrieving data.

顺便说一句,“不能发送尽可能多的信息”不是使用 POST 的原因。使用 POST 的原因是该请求将修改您站点上的数据,而不是简单地检索数据。

Suppose you put a hyperlink on your page with a GET request like "/delete_user?id=1234," and then some search engine innocently follows the link as it's indexing your site. That's why GET requests are not good for requests that modify data.

假设您在页面上放置了一个带有 GET 请求(如“ /delete_user?id=1234,”)的超链接,然后某个搜索引擎在为您的网站编制索引时无意中跟踪了该链接。这就是 GET 请求不适用于修改数据的请求的原因。

回答by Alex Pop

To avoid issues with some proxies and Apache rewrites, pass in POSTdata or set the Content-Length: 0header for requests with an empty body.

为避免某些代理和 Apache 重写的问题,请传入POST数据或Content-Length: 0为具有空主体的请求设置标头。

I recently had issues with Apache converting my request to a GETwhen doing a POSTwith an empty body. So, instead of this:

我最近在 Apache 将我的请求转换为 aGET时遇到了问题,在执行POST空主体时。所以,而不是这个:

 curl -X POST https://example.com/api/user/123456789

pass the Content-Lengthheader:

传递Content-Length标题:

 curl -X POST https://example.com/api/user/123456789 -H 'Content-Length: 0'

or pass something in the body:

或在体内传递一些东西:

 curl -X POST https://example.com/api/user/123456789 -d ''

回答by Kamal Kumar

I want to redirect user_login.php to seo friendly url like /user-login with post form data and this worked for me.

我想将 user_login.php 重定向到 seo 友好的 url,例如 /user-login 和表单数据,这对我有用。

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/user_login\.php [NC]
RewriteRule ^ user-login [QSA,R=301]
RewriteRule ^user-login$ user_login.php [QSA,L]

In view file

在视图文件中

<form action="<?php $siteurl;?>/user-login" method="post" id="user_login">

回答by mcrumley

As long as you are only using an internal rewrite, not an HTTP redirect, you should not lose POST data. Here is the rule I use on my site:

只要您只使用内部重写,而不是 HTTP 重定向,就不会丢失 POST 数据。这是我在我的网站上使用的规则:

RewriteRule ^(.*)$ index.php/ [L]

Try using the HTTPLiveHeaders extension for Firefox (or something similar) and track the entire page request. Make sure you are not getting an HTTP redirect. If you get a HTTP/1.1 3xxresponse and Location: http://addressheader, that is the problem. Your rewrite rule that you posted should not cause that to happen. If you are being redirected, there is probably either an error in your PHP code or another rewrite rule that is being applied.

尝试使用 Firefox 的 HTTPLiveHeaders 扩展(或类似的东西)并跟踪整个页面请求。确保您没有收到 HTTP 重定向。如果您收到HTTP/1.1 3xx响应和Location: http://address标头,那就是问题所在。您发布的重写规则不应导致这种情况发生。如果您被重定向,则可能是您的 PHP 代码中存在错误或正在应用其他重写规则。