Apache VirtualHost 重定向主机 + 路径

Apache VirtualHost 重定向主机 + 路径

我需要重定向所有请求http://example.com/apphttp://app.example.com/。在 VirtualHost 中,我只能指定 ServerName 作为 example.com。我只希望来自主机加上特定路径的请求重定向到带有子域的域。

<VirtualHost *:80>
ServerName example.com/app
ProxyPreserveHost Off
ProxyPass / http://app.example.com/
ProxyPassReverse / http://app.example.com/
</VirtualHost>

答案1

如果我正确理解了你想要做的事情,那么应该这样做:

<VirtualHost *:80> ServerName example.com ProxyPreserveHost Off ProxyPass /app/ http://app.example.com/ ProxyPassReverse /app/ http://app.example.com/ RedirectMatch ^/app$ /app/ </VirtualHost>

由于您可能已经拥有 example.com 的虚拟主机,因此您应该将这些Proxy*行添加到现有配置中。

答案2

最后使用了 apache 重写。因为我使用 apache 将请求转发到 tomcat 应用程序,所以我没有 .htaccess 文件,而大多数教程都会告诉您要将重写放在这个文件中。事实证明,您可以直接将其包含在虚拟主机标记中,我就是这么做的。这是一个工作示例。

<VirtualHost *:80>
ServerName example.com/app
ProxyPreserveHost Off
ProxyPass / http://app.example.com:8080/AppName/
ProxyPassReverse / http://app.example.com:8080/AppName/

# Rewrite all request from example.com/app to app.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/app/?(.*) http://app.example.com/$1 [R,L]
</VirtualHost>

相关内容