apache rewrite with proxypass

2019-09-16 15:14发布

问题:

I am trying to combine a rewrite and a proxy pass and having issues with the rewrite. Here is what I have

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.world.net
RewriteRule %{HTTP_HOST} http://newexample.newworld.net:7802/apex/f?p=208 [R,P]
ProxyPass / http://newexample.newworld.net:7802/

The proxypass is working, but I can't figure out how to get the initial redirect. So if the user puts in example.world.net/apex/f?p=208 it goes to newexample.newworld.net:7802/apex/f?p=208 and masks the url.

However I need to get example.world.net to redirect to example.world.net/apex/f?p=208 if the apex/f?p=208 is not in the url.

回答1:

You can't redirect and proxy at the same time. Your rewrite rule flags are [R,P] which is "redirect" and "proxy". You'll need one or the other here. Also, your rule's regex is never going to match %{HTTP_HOST}, unless your URL is literally: http://example.world.net/%{HTTP_HOST}. You'll need to change it to:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.world.net
RewriteCond %{QUERY_STRING} !(^|&)p=208(&|$)
RewriteRule ^/?$ /apex/f?p=208 [L,QSA,R]

This will redirect the browser if the URL address bar says http://example.world.net/ to http://example.world.net/apex/f?p=208. Then, it's up to the proxy to take /apex/f?p=208 and proxy it to http://newexample.newworld.net:7802/.

There's a possibility that mod_proxy and mod_rewrite won't play nicely together because both can end up getting applied to the same URL. If you find that both are getting applied at the same time, then change the ProxyPass line to:

RewriteRule ^/?(.*)% http://newexample.newworld.net:7802/$1 [L,P,QSA]


回答2:

So you want example.world.net to redirect to http://newexample.newworld.net:7802/apex/f?p=208 or to example.world.net/apex/f?p=208 ? I assume the former, if I'm wrong change url in RewriteRule to the latter.

but I think this should do it

RewriteCond %{HTTP_HOST} ^example.world.net$ [NC]
RewriteRule %{HTTP_HOST} http://newexample.newworld.net:7802/apex/f?p=208 [R,L]

but then it is unknown what is the Name/Aliases of your virtual host, so that / in

ProxyPass / http://newexample.newworld.net:7802/

might break it all.