Mod-rewrite rule for external pages?

2019-02-12 16:07发布

问题:

Is it possible use mod_rewrite to resolve addresses hosted on another server?

Say I want to setup this URL:

http://www.myserver.com/myfolder/

To actually resolve to:

http://www.anotherserver.com/anotherfolder/

If so, could you provide a RewriteRule example?

回答1:

You can use the P flag in a mod_rewrite rule to get that substitution URL requested by mod_proxy:

RewriteEngine on
RewriteRule ^myfolder/$ http://other.example.com/anotherfolder/ [P]

Now when a client is requesting /myfolder/ from your server, it will request http://other.example.com/anotherfolder/ and send the response from that server back to the client.



回答2:

No, tunneling is not possible, you'd have to use a CGI script for this. However, you can redirect:

RewriteRule  ^(.*)  http://new.example.com/$1

with or without the [R] flag, and it will automatically redirect the user to the new domain.

Edit: Apparently it is possible to tunnel requests with mod_proxy and the [P] flag. See Gumbo’s answer.



回答3:

Hey I tried rewrite and didn't work (just tested in localhost)

Also I find this easy way

Redirect /myfolder external_url