Search code examples
apachemod-rewritemod-proxy

Apache mod_rewrite and multiple domains


I'm trying to use mod_rewrite to map multiple domains to different servlets on one host. Example:

www.dom1.com -> 192.168.1.n/dom1

www.dom2.com -> 192.168.1.n/dom2 ...

I'm using the mod_rewrite and mod_proxy and VirtualHost directive but it seems that the reverse mapping via ProxyPassReverse doesn't work as I expected.

ProxyPassReverse /subdomain.domain.com http://192.168.1.n/subdomain

doesn't work. I've turned rewrite-logging on with

RewriteLog /var/log/rewrite.log

From the logs I'd say that rewriting works and the problem seems to be with reverse mapping. However I can't see any Reverse mapping entries. It seems that reverse mapping isn't logged or needs a different command to be activated. (Apache and the servlet container are on different machines but this should not matter I'd think ?)


Solution

  • After all I've found a solution that works for me. This is an excerpt from my configuration that shows one virtual host for domain 1

    <VirtualHost *>
      ServerName www.dom1.com
      ServerAlias dom1.com
    
      RewriteEngine On
    
      # logs might be omitted
      RewriteLog /var/log/dom1_rewrite.log
      RewriteLogLevel 2
      CustomLog /var/log/dom1_custom.log common
      ErrorLog /var/log/dom1_error.log
    
      # rewrite to internal ip
      RewriteRule ^/(.*) http://192.168.1.105/dom1/$1 [L,P,E=proxy_ok:1]
    
      # Preserve the host-part in the forwarded url
      ProxyPreserveHost On
    
      # Substitute responses with the original
      ProxyPassReverse / http://192.168.1.105/dom1/
      ProxyPassReverse / http://192.168.1.105/dom1
      ProxyPassReverse / http://dom1.com/dom1/
      ProxyPassReverse / http://dom1.com/dom1
    </VirtualHost>
    

    What was wrong with my first configuration - I had to preserve the host and then add all necessary ProxyPassReverse rules to substitute the responses.

    And this is my mod_proxy configuration:

    <IfModule mod_proxy.c>
            #turning ProxyRequests on and allowing proxying from all may allow
            #spammers to use your proxy to send email.
    
            ProxyRequests Off
    
            <Proxy *>
                    AddDefaultCharset off
                    Order deny,allow
                    Deny from all
    
                   # Proxies just in case Proxy_ok is set
                   Allow from env=proxy_ok
            </Proxy>
    
            # Not sure whether we need this ...
            # Enable/disable the handling of HTTP/1.1 "Via:" headers.
            # ("Full" adds the server version; "Block" removes all outgoing Via: headers)
            # Set to one of: Off | On | Full | Block
            ProxyVia On
    </IfModule>
    

    There may be cleaner solutions but - if works as it should.