多个URL重定向htaccess与POST数据

时间:2014-02-05 05:53:41

标签: php .htaccess post

在我的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]

此代码正在运行,但它提供了错误的网址。在这种情况下,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

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

1 个答案:

答案 0 :(得分:1)

如上所述,POST如果进行外部重定向,数据将会丢失。

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

  1. 不要重定向,只进行内部重写
  2. 启用mod_proxy并使用P标志代理请求
  3. 编写服务器端代码,例如使用curl并将POST数据传递到目标
  4. DOCUMENT_ROOT/.htaccess文件中对内部重写试用此规则:

    RewriteEngine On
    
    RewriteRule ^(?:client2|client1)/a_report/(.+)$ /commonsetup/$1 [NC,L]
    
相关问题