1

我对我的服务器有这种形式的查询:

http://my.host.com/moo?p=1&s=2&targetURL=http://foo.com/sub/more/query

我需要做的是替换这个查询的一部分参数,特别是我需要用 替换http://foo.com/sub/http://bar.com/保留这个查询中的所有其他参数,同时保留http://foo.com/sub/more/query. 我会满足于将它替换为targetURL,所以我尝试了这个:

RewriteCond %{REQUEST_URI} ^/moo
RewriteRule ^/moo(.*)targetURL=http://foo.com/sub(.*)$ http://other.host.com/moo$1targetURL=http://bar.com$2 [L,R]
RewriteRule ^/moo(.*)$ http://other.host.com/moo$1 [L,R]

但是第一个查询永远不会匹配。有什么帮助吗?

编辑:为了清楚起见,我有这个:

http://my.host.com/moo?p=1&s=2&targetURL=http://foo.com/sub/more/query

我希望它变成这样:

http://other.host.com/moo?p=1&s=2&targetURL=http://bar.com/more/query
4

2 回答 2

1

尝试这个:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^my\.host\.com [NC]
RewriteCond %{QUERY_STRING} ^(.*)targetURL=http://foo.com/sub/(.*)$ 
RewriteRule ^/?moo$ http://other.host.com/moo?%1targetURL=http://bar.com/%2 [L,R=301]

您需要targetURL=http://foo.com/sub/使用. 与它匹配时,查询字符串不是 URI 的一部分。然后,您可以使用和反向引用来引用查询字符串中匹配的分组。%{QUERY_STRING}RewriteCondRewriteRule%1%2

于 2012-08-13T20:00:13.960 回答
1

试试这个我测试过并适合我的版本。

<i> 

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^my\.host\.com/moo [NC,OR]
    RewriteCond %{QUERY_STRING} ^p\=1\&s=2\&targetURL\=http\:\/\/foo.com/sub/more/query$ [NC,OR]  
    RewriteRule ^/(.*) http://other.host.com/moo?p=1&s=2&targetURL=http://bar.com/more/query/$1 [R=301,L]
  </IfModule>

于 2012-08-13T16:17:18.620 回答