Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a Apache Webserver with mod_proxy and a Tomcat server with my Grails web app runnning. The Apache is redirecting with proxy the request to to my Tomcat running on

I need to redirect requests the following way:

from apache should get per proxy to

How can I archieve this with the following apache2 config:

NameVirtualHost *:80

<VirtualHost *:80>
        <Proxy *>
                Order deny,allow
                Allow from all

        # compress text, html, javascript, css, xml:
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript

        ExpiresActive On
        ExpiresByType text/html "access plus 7 days"
        ExpiresByType image/gif "access plus 1 months"
        ExpiresByType image/jpeg "access plus 1 months"
        ExpiresByType image/png "access plus 1 months"
        ExpiresByType text/css "access plus 14 days"
        ExpiresByType text/javascript "access plus 14 days"
        ExpiresByType application/x-javascript "access plus 14 days"
        ExpiresByType image/ico "access plus 1 months"

        RewriteEngine on
        # www to non-www using search-engine friendly 301
        RewriteCond %{HTTP_HOST} ^ [NC]
        RewriteRule ^(.*)$$1 [L,R=301]

        # IP canonicalization
        RewriteCond %{HTTP_HOST} ^1\.2\.3\.4
        RewriteRule ^(.*)$$1 [L,R=301]

        <Directory />
                Options FollowSymLinks
                 AllowOverride None
        <Directory /opt/example/web-app>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all

        # redirect everything else to TOMCAT
        ProxyPass               /
        ProxyPassReverse        /
        ProxyPassReverseCookieDomain localhost
        ProxyPreserveHost On

Thank you for any help!

share|improve this question
up vote 0 down vote accepted

Ok, I got it running...

The key elements where the following mod_rewrite conditions

    RewriteCond   %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
    RewriteCond %{REQUEST_URI} ^/some.+$
    RewriteRule   ^(.+)  %{HTTP_HOST}$1  [C]
    RewriteRule   ^([^.]+)\.example\.com(.*)$2-$1 [P]

The first condition tells the Apache server to catch all URLs which address a subdomain - except the subdomain www.

The second condition tells the Apache that the requested path (URI) must start with a special indicator. In my example this is "some". This is important, otherwise all css/images/js etc. falls under this rewrite condition and my tomcat wouldn't deliver all that stuff.

now there are two lines of rewrite rules. The first one matches the subdomain and save it in var $1. The second one matches the URI and saves it in $2. After the second rewrite rule I tell the system how to build up the new URL:$2-$1 [P]

Here I redirect to the tomcat server runnning on my system. It might be ok to use an IP instead of the domain name. The [P] tells the system to use the mod_proxy to redirect the traffic.

In the last line we define a ProxyPassReverse to re-build all links in the answer-document coming from the proxy and going to the client browser - so all anchors going back to

I had some problems with my tomcat config when using the above code. The [P] flag doesn't preserve the original host. Because of that I had to change in the tomcat server.xml the default host for my used engine:

 <Engine name="Catalina" defaultHost="">

Hopefully these infos help someone at some time..

Best wishes

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.