使用Location proxyPass将api调用从子域redirect到主域

我想将我的api调用从子域redirect到主域。 我的API服务器是在example.com。 请求url是http://foo.example.com/api?service=work&action=all_work ,但我的文件系统调用应该通过。

<VirtualHost *:80> ServerName example.com ServerAlias foo.example.com RewriteEngine On <Proxy *> Order Allow,Deny Allow from all </Proxy> <Location /api> ProxyPass http://othersite.com/api retry=0 SetOutputFilter DEFLATE </Location> DocumentRoot /var/www/example RewriteCond %{HTTP_HOST} ^(foo.example.com)$ [NC] RewriteCond %{REQUEST_URI} =/ RewriteRule (.*) /var/www/example/index.html [L] RewriteCond %{HTTP_HOST} ^(foo.example.com)$ [NC] RewriteCond %{REQUEST_URI} !=/ RewriteRule (.*) /var/www/example/$1 [L] </VirtualHost> 

但是我正在浏览器上获得404。

代替使用代理传递,我们可以使用RewriteCond和RewriteRule对它进行细粒度控制,并使用[P] mod_proxy标志

 RewriteCond %{HTTP_HOST} ^(monitor.whatfix.com)$ [NC] RewriteCond %{REQUEST_URI} =/api RewriteRule (.*) http://othersite.com/api [P,L] 

如果请求来自子域,那么我将其重写到另一个站点。