1

I would like to automatically redirect requests for http://example.com to http://example.com/SomePage

I am able to do that with Apache with the following rule:

RewriteRule ^/$ /SomePage [R]

However, some of my servers do not run on top of Apache, just Tomcat. How do I implement the equivalent in web.xml? SomePage is a struts2 action.

zb226
  • 9,586
  • 6
  • 49
  • 79
brendangibson
  • 173
  • 1
  • 4
  • 9

2 Answers2

2

If you are using Tomcat alone instead of with Apache with modrewrite, the best approach is to install a rewrite filter and reference it from your web.xml. One filter I have used in the past is UrlRewriteFilter from tuckey.org (http://www.tuckey.org/).

It is on Google Code at http://code.google.com/p/urlrewritefilter/source/browse/trunk/src/test-web/WEB-INF/urlrewrite.xml.

Last update was in October 2010, but I do remember it working just fine. Config files are XMLish, not the nice one liners that you use in modrewrite.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • I was trying to avoid that if possible. I could write my own servlet to do the redirect, which I was trying to avoid as well. – brendangibson Jul 09 '11 at 00:11
  • 1
    Sorry, I don't think there is any native Tomcat solution for URL rewriting. I think you will find UrlRewrite easy to use. Perhaps someone else has another solution? – Ray Toal Jul 09 '11 at 06:59
  • 2
    Why not use an index page that redirects to your first struts action, place the index page in your war file and create a `welcome` section in your web.xml. It's one more server roundtrip but easy to implement using standard servlet API functionality – home Jul 10 '11 at 11:28
  • @home Good point, that definitely works for redirecting the welcome page using only the servlet API. Guess I was thinking of a more general solution. +1 – Ray Toal Jul 10 '11 at 16:56
-1

I would just use <welcome-file-list> in the web.xml. For example

<welcome-file-list>    
  <welcome-file>SomePage.html</welcome-file>
</welcome-file-list>
duvo
  • 1,634
  • 2
  • 18
  • 30