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

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 : to redirect them to the www sub domain?

any help would be greatly appreciated.

share|improve this question

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

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

In Tomcat (servlets):

servletResponse.setHeader("Location", "");

In JavaScript:

<script type="text/javascript">
    window.location = ""

In plain HTML:

<meta HTTP-EQUIV="REFRESH" content="0; url=">
share|improve this answer
This doesn't seem to answer the question at all: First it does not solve it they way the asker wants, secondly the attempted solutions will unconditionally redirect, not only when accessing the bare domain. – Erik I Jun 23 '15 at 11:58

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.