Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is there a way using the virtual server tags in the server.xml file in tomcat to redirect anyone who accesses the domain directly (eg : http://example.com) to redirect them to the http://www.example.com www sub domain?

any help would be greatly appreciated.

share|improve this question

migrated from stackoverflow.com Mar 16 '11 at 13:46

This question came from our site for professional and enthusiast programmers.

possible duplicate of Redirect in Tomcat 5.5 –  Gabe Mar 16 '11 at 7:38

4 Answers 4

In Tomcat (servlets):

servletResponse.setHeader("Location", "http://www.domain.com");

In JavaScript:

<script type="text/javascript">
    window.location = "http://www.domain.com"

In plain HTML:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.domain.com">
share|improve this answer

If you need server-side redirection in Tomcat from one URL to another, you have several options:

  1. Some simple redirections might be performed by Load Balancer web application shipped with Tomcat 5.5; it looks like later Tomcat releases have abandoned it.
  2. If your needs for URL redirection may be limited to web application level, then UrlRewriteFilter from www.tuckey.org is the powerful tool, most probably to help you.
  3. If your needs are better fit with server/container level redirection, then the right tool is probably the RewriteValve, a component of JBoss Web Server, comparable in power with Apache httpd mod_rewrite.
  4. If you need server-level redirection, but prefer something more light-weight, you may wish to have a look at my Tomcat Redirect Valve.
share|improve this answer

Typically this would be done using an Apache front end, but you could use named hosts in your context configuration so specify different contexts, where the non-www version just redirects to the www version. Apache is much simpler in my opinion.

share|improve this answer

You don't need tomcat to do this, you can do this directly in the DNS entry.

share|improve this answer
He's looking for URL rewriting rules, not DNS hacks. But technically, yes, he could do this with DNS –  pk. Mar 16 '11 at 14:18
@pk: Actually, doing this in Tomcat via URL-rewriting is a hack. DNS is the proper way. –  Quandary Apr 9 '11 at 21:13

Your Answer


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