Search code examples
apachemod-proxy

Apache Redirect Request From Domain + Path


I am using apache in front of tomcat so I can redirect requests from the domain to the tomcat url using mod_proxy. I've run into an issue on the server where when using a subdomain it wants to go back to the base domain. Is there any way to redirect all requests from example.com/foo to foo.example.com?

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

The example above is not working. I presume that is because I cannot include a path in the servername.

Here is my working version of the tag based on the accepted answer.

<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/foo to foo.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/foo/?(.*) http://foo.example.com/$1 [R,L]
</VirtualHost>

Solution

  • Speaking strictly to the last statement in your original post, "Is there any way to redirect all requests from example.com/foo to foo.example.com".

    You can use a rewrite to accomplish this. For example:

    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^/foo/?(.*) http://foo.example.com/$1 [R,L]

    This assumes that your rewrite module is loaded and enabled:

    LoadModule rewrite_module modules/mod_rewrite.so
    RewriteEngine On