问题
I'm trying to get an older Apache (2.2.17) to proxy:
http://foo.com/proxy/?url=http%3A%2F%2Fbar.com%2foo
to:
http://bar.com/foo
I have:
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^/proxy/ %1? [P,L]
Unfortunately, this results in Apache trying to proxy the URL-encoded value (log output) :
(3) applying pattern '^/proxy/' to uri '/proxy/'
(4) RewriteCond: input='url='http%3A%2F%2Fbar.com%2foo'' pattern='^url=(.*)$' => matched
(2) rewrite '/proxy/' -> ''http%3A%2F%2Fbar.com%2foo'?'
(3) split uri='http%3A%2F%2Fbar.com%2foo'? -> uri='http%3A%2F%2Fbar.com%2foo', args=
(2) forcing proxy-throughput with http://foo.com/'http%3A%2F%2Fbar.com%2foo'
(1) go-ahead with proxy request proxy:http://foo.com/'http%3A%2F%2Fbar.com%2foo' [OK]
So, it appears there are two problems. One is that there are apostrophes in the result and the other is that the result is not URL decoded. I assume the reason Apache is prepending the original protocol://host is that it doesn't see the result as an URL.
回答1:
If you have access to vhost/server config (and it looks like you do), you can configure one of apache's built in rewrite maps to unescape for you:
RewriteMap unescape int:unescape
Then you can use the map in your rules:
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^/proxy/ ${unescape:%1}? [P,L]
来源:https://stackoverflow.com/questions/12416434/how-can-i-proxy-a-query-string-url-in-apache