0

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.

1

4 Answers 4

1

In Tomcat (servlets):

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

In JavaScript:

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

In plain HTML:

<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.domain.com">
1
  • 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, 2015 at 11:58
1

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

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.

0

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

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

You must log in to answer this question.