我有一个规则,例如:
RewriteCond %{HTTP_HOST}:%{QUERY_STRING} ^(\w+)\.t(.*)\.(\w+)?\.sprawk\.com:(.*)$
RewriteRule ^(.*) /api/translateHtmlPublic?tl=%1&su=%2&p=$1&q=%4 [B,PT,L]
这对于 GET 请求非常有用。
但是,对于 POST 请求,q 参数为 null(这是我所期望的),但在我的 Java servlet 内,HttpServletRequest.getReader()
和 HttpServletRequest.getInputStream()
都没有提供数据。
使用 Firefox Live header ,我看到我正在发送:
POST /uid/contactSend.cfm HTTP/1.1
Host: fr.t51940.local.sprawk.com
Connection: keep-alive
Referer: http://fr.t51940.local.sprawk.com/uid/contact.cfm?country=137
Cache-Control: max-age=0
Content-Type: application/x-www-form-urlencoded
Content-Length: 146
userName=Nic&queryCountry=Libya&queryCode=137&userEmail=n.cottrell%40idea.int&Comment=Just+testing&captcha=Quotas&Submit=--+Envoyer+Commentaire+--
我知道 servlet 正在将其作为 POST
接收,因为 doPost
方法被触发。我知道 Apache 2.x 应该在简单重写期间自动保留 POST 数据,但也许添加参数会删除内容数据?
更新:我刚刚意识到(通过转储所有参数)userName、queryCountry、queryCode 参数通过 request.getParameter(String)
方法获取正确的值。问题是我需要能够将原始表单参数名称与我在 RewriteRule 中引入的参数名称分开。
最佳答案
POST 的参数位于正文中,而不是 header 中。您可以根据需要重写 URL,而不会影响它们。
内容类型为
application/x-www-form-urlencoded
的 POST 请求正文仅包含通过request.getParameter() 检索的参数。
通过request.getInputStream()
和request.getReader()
读取正文不会传递任何内容。 (Servlet 规范 3,#3.1.1)根据 Servlet 规范 #3,添加到 URL 的参数应该可以通过
getParameter()
检索。无法区分正文中的 POST 参数和 header 中的 URL 参数。因此,您的解决方案或问题可能有问题。
关于java - 我可以重写 URL、添加查询参数并保留表单数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13753164/