0

I need to redirect all requests from http://example.com/app to http://app.example.com/. In VirtualHost I can only specify a ServerName as example.com. I only want requests from the host plus a specific path to be redirected to the domain with subdomain.

<VirtualHost *:80>
ServerName example.com/app
ProxyPreserveHost Off
ProxyPass / http://app.example.com/
ProxyPassReverse / http://app.example.com/
</VirtualHost>
2
  • When you say redirect, do you mean proxy all request of example.com/app to app.example.com? Or do you mean return a 301 Moved Permanently response to the browser with the app.example.com location? Jan 22, 2016 at 21:46
  • Proxy the requests. The application infrastructure doesn't account for the subdomain so it uses /app instead, but on the front end I want the subdomain. In other words the application is redirecting to /app but I want it to appear as app.example.com.
    – ryandlf
    Jan 23, 2016 at 3:58

2 Answers 2

0

If I understand correctly what you're trying to do, this should do it:

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

Since you probably already have a virtualhost for example.com, you should add the Proxy* lines to the existing config.

0

Ended up using apaches rewrite. Because I am using apache to forward requests to a tomcat application I do not have an .htaccess file which is where most tutorials tell you to place the rewrite. Turns out you can include it right in the virtualhost tag which is what I did. Here is a working sample.

<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>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .