首先,您需要确保mod_proxy 已启用。然后,假设您希望每个/api/、/appname/ 和/admin/ 看起来像最终用户的子目录,并且他们分别在2001、3001 和4001 上收听,我认为你正在寻找的是这样的:
(在服务器或虚拟主机配置中)
RewriteRule ^/api/(.*) http://localhost:2001/$1 [P,L,QSA]
ProxyPassReverse /api/ http://localhost:2001/
RewriteRule ^/appname/(.*) http://localhost:3001/$1 [P,L,QSA]
ProxyPassReverse /appname/ http://localhost:3001/
RewriteRule ^/admin/(.*) http://localhost:4001/$1 [P,L,QSA]
ProxyPassReverse /admin/ http://localhost:4001/
(在 .htaccess 配置中......没有前导斜杠......并且没有 mod_proxy)
RewriteRule ^api/(.*) http://localhost:2001/otherpath/$1 [P,L,QSA]
RewriteRule ^appname/(.*) http://localhost:3001/otherpath/$1 [P,L,QSA]
RewriteRule ^admin/(.*) http://localhost:4001/otherpath/$1 [P,L,QSA]
$1 代表括号捕获的内容。 P 告诉 mod_rewrite 使用 mod_proxy 来处理这个重写。 L 表示将此作为最后一条规则;应用后停止重写。 QSA 将来自入站请求的查询字符串附加到重写的请求上。
ProxyPassReverse 用于捕获来自内部实例的任何重定向并将其重写以匹配外部 URI。注意:mod_proxy 需要在服务器配置(服务器、虚拟主机、目录)中,并且在.htaccess 中无效
一些有用的参考:
现在,说了这么多,您可能无论如何都不想使用mod_rewrite 来执行此操作。确实是mod_proxy 为您完成所有工作。如果你没有任何实际的重写,你应该直接使用mod_proxy。即使是mod_rewrite 也只是将其交给mod_proxy。
来自http://httpd.apache.org/docs/2.2/rewrite/avoid.html:
RewriteRule 提供 [P] 标志来传递重写的 URI
mod_proxy.
RewriteRule ^/?images(.*) http://imageserver.local/images$1 [P]
但是,在许多情况下,当没有实际的模式匹配时
需要,如上例所示,ProxyPass 指令是
更好的选择。这里的示例可以呈现为:
ProxyPass /images/ http://imageserver.local/images/
请注意,无论您使用 RewriteRule 还是 ProxyPass,您仍然会
需要使用ProxyPassReverse 指令来捕获重定向
从后端服务器发出:
ProxyPassReverse /images/ http://imageserver.local/images/
当有其他的
RewriteRule 在同一范围内有效,因为 RewriteRule 将
通常在 ProxyPass 之前生效,因此可能会抢占什么
你正在努力完成。