0

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>
ryandlf
  • 27,155
  • 37
  • 106
  • 162

2 Answers2

0

You could use mod_rewrite within apache. Ensure it is enabled, as root:

a2enmod rewrite

That will confirm it is has been enabled or already is. If it has been enabled you'll need a restart of Apache.

Then in your root of your virtualhost create/edit .htaccess file and add the line:

Redirect /foo/ http://foo.example.com/

Depending on your specific directory permissions you may need to allow overrides for the directory

<Directory /your/path/here>
  AllowOverride All
</Directory>

HTH

Graeme
  • 1,643
  • 15
  • 27
  • I don't actually have anything living inside apache. All i'm using apache for is to redirect to my tomcat app. Will this still work in that situation? – ryandlf Jan 22 '16 at 19:32
  • Also will this preserve the remaining parts of the path? example.com/foo/more to foo.example.com/more. – ryandlf Jan 22 '16 at 19:35
  • Hmm possibly not, sorry. I am sure you could use Mod_rewrite to achieve your goals, although might be worth reading this post, may help with your problem better? http://stackoverflow.com/questions/603765/how-do-i-redirect-from-apache-to-tomcat – Graeme Jan 22 '16 at 20:13
0

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

Brandon Harris
  • 962
  • 7
  • 6
  • I'm struggling with how to use rewrite because this isn't actually an apache site. Apache literally just sits in front and forwards the requests to a tomcat application url. – ryandlf Jan 23 '16 at 04:01
  • Scratch that. This worked. Instead of including the rewrite within an .htaccess (because I don't have one) I simply included it directly in the virtual host. I'm going to include the working virtualhost tag in the original post. – ryandlf Jan 23 '16 at 05:12