views:

1337

answers:

2

Hi,

I have a Tomcat (6.0.20) and Apache Server setup (2.2) and am trying to redirect all subdomains to a specific context, on my domain.

e.g., s.example.com redirect to www.example.com

Apache is fowarding requests via mod_jk (I tried mod_proxy, but the css and js didn't load as its not absolute urls).

My current setup:

httpd.conf:

Include C:/apache-tomcat-6.0.20/conf/auto/mod_jk.conf
RewriteEngine On
<VirtualHost *:80>
    ServerName www.example.co.za
    ServerAlias www.example.co.za example.co.za *.example.co.za
    RewriteEngine on
    RewriteLog "C:/Program Files/Apache Software Foundation/Apache2.2/logs/rewrite.log" 
    RewriteLogLevel 3 
    RewriteCond %{HTTP_HOST} example\.co\.za.*$ [NC]
    RewriteRule ^(.*)$ http://www.example.co.za [L] 
    JkMount /* worker1
</VirtualHost>
JkMount /* worker1

server.xml:

    <Host name="www.example.co.za" appBase="hosts/example"
      unpackWARs="true" autoDeploy="true"
          xmlValidation="false" xmlNamespaceAware="false">
       <Valve className="org.apache.catalina.valves.AccessLogValve"
                directory="C:/apache-tomcat-6.0.20/logs" prefix="localhost_access_log."
                suffix=".txt" pattern="common" resolveHosts="false"/>

       <Context path="" docBase="Property"/>
        <Alias>*.example.co.za</Alias>
     </Host>

The redirect for ww.example.com is going into a non-stop redirect loop.

This is extremely important from a security point of view as the user could access the tomcat manager and other apps on the server (namely hudson).

A: 

Try this rule:

RewriteCond %{HTTP_HOST} !^www\.example\.co\.za$
RewriteRule ^ http://www.example.co.za [L]

And if you want to keep the requested URI:

RewriteCond %{HTTP_HOST} !^www\.example\.co\.za$
RewriteRule ^ http://www.example.co.za%{REQUEST_URI} [L]

Additionally I recommend you to use a 301 redirection. So add the R flag with the value 301 by replacing [L] with [L,R=301].

Gumbo
incredibly simple. thanks
RaelG
A: 

Hi Gumbo, If i hit http://abc.com which is the required url to hit which is on domain zzz.com domain. And my web server resides on ppp.com domain. Do i require to have the above required code to used. please tell since i am new to apache and tomcat confg.

thanks shiju