【问题标题】:multiple URL redirect htaccess with POST data带有 POST 数据的多个 URL 重定向 htaccess
【发布时间】:2014-02-05 05:53:40
【问题描述】:

在我的 REST API 旧版本项目中,我有多个设置。在较新的版本中,我有通用设置代码而不是多个设置。

我必须编写 htaccess 规则来将旧 URL 重定向到新版本 URL。所有请求都是 POST 请求,所以我必须在不影响 POST 数据的情况下进行重定向。

问题

旧设置网址

http://example.com/client1/a_report/index.php/rest_server/index http://example.com/client2/a_report/index.php/rest_server/index

我希望将这些网址重定向到最新的网址

http://example.com/commonsetup/index.php/rest_server/index

我尝试了以下代码,但没有按预期工作

RewriteEngine On
RewriteBase /
RewriteRule ^client1/a_report(.*) http://example.com/commonsetup/$1 [NC,L,P]
RewriteRule ^client2/a_report(.*) http://example.com/commonsetup/$1 [NC,L,P]

此代码有效,但给出的 URL 错误。在这种情况下,POST 将变为 GET。这也是我需要解决的

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/client1/
RewriteRule (.*) /commonsetup/$1 [R=301,L]

我在浏览器中得到的结果

未找到请求的 URL /commonsetup/client1/a_report/index.php/rest_server/index 不是 在此服务器上找到。

需要的输出是

/commonsetup/index.php/rest_server/index

请帮助我实现这一目标。提前致谢。

【问题讨论】:

  • POST 数据在重定向后不会传输。通过服务器端的 curl 来完成。
  • 我有 50 个设置,我必须摆脱这个,这就是为什么那个地方的公共代码。好的如何重定向
  • 只要您的重写规则实际上并未重定向页面(而只是在内部重写路径),您的 POST 数据就会被保留,因为它仍在使用相同的 HTTP 请求。 - 从谷歌收集

标签: php .htaccess post


【解决方案1】:

正如我在上面POST 评论的那样,如果进行外部重定向,数据将会丢失。

您有 3 个选项来保留 POST 数据:

  1. 不做重定向,只做内部重写
  2. 启用mod_proxy 并使用P 标志来代理请求
  3. 编写服务器端代码,例如使用 curl 并将 POST 数据传递到目的地

在您的DOCUMENT_ROOT/.htaccess 文件中尝试使用此规则进行内部重写

RewriteEngine On

RewriteRule ^(?:client2|client1)/a_report/(.+)$ /commonsetup/$1 [NC,L]

【讨论】:

  • 如何解决我在尝试中遇到的错误。我写了给出错误的重写规则
  • 你和我的第二次尝试差不多。我从浏览器收到此消息 在此服务器上找不到请求的 URL /commonsetup/client1。但我想要这样的 URL /commonsetup/index.php/rest_server/index
  • 如果你的原始 URL 有 index.phphttp://example.com/client1/a_report/index.php/rest_server/index 那么它将被重写为:http://example.com/commonsetup/index.php/rest_server/index
  • 其实我有一个错误我更正了,现在试试吧。
猜你喜欢
  • 2018-08-27
  • 2022-07-01
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
  • 2022-12-29
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
相关资源
最近更新 更多