1

I'm curious on what's the best way to do an URL Rewrite, in a java application. Say you have an app called test_app, and I deploy it on tomcat.

By URL Rewrite I mean that instead of accessing the app via host:port/test_app/ , I want to access it by doing host:port/fancy_URL_for_test_app/ .

Question : where is it better to do the URL Rewriting ? On tomcat container, or by using something along the lines of tuckey URLRewriteRule (meaning, rewriting at application level).

It would seem to me that rewriting on tomcat level would be the best way to go, from a design point of view, however I've noticed different opinions here, such as Is there a url rewriting engine for Tomcat/Java? .

Could you please explain why would you choose to do the rewriting on the application level instead of container level ?

NOTE : I'm talking strictly about root context rewrite (i.e. changing host:port/test_app/ into host:port/fancy_root_context/ rather than changing host:port/test_app/login1 into host:port/test_app/login2

Thanks in advance ! Andrei

Community
  • 1
  • 1
Andrei
  • 1,613
  • 3
  • 16
  • 36

1 Answers1

2

In most cases there is something before Tomcat (or any other Java EE server) - like nginx, apache etc. I would handle rewriting there.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Maciej Dragan
  • 485
  • 3
  • 5
  • Sorry, the whole point of the question is WHY. And assuming you don't have httpd in front of tomcat, would the answer change, from the conceptual point of view ? Of course, httpd makes it easier to rewrite, but tomcat also allows you to do rewrites. Ignoring the fact that is slightly difficult in tomcat, could you present arguments for your answer ? – Andrei Oct 03 '12 at 08:57
  • Plain http servers like apache and nginx are designed to be extremely fast and lightweight plus do some small tasks like URL rewriting. Handling it in this layer will result in faster requests processing, not mentioning than you could handle scaling better when traffic grows. Handling rewrites on application level will give you more possibilities but the question is - do you really need it? – Maciej Dragan Oct 03 '12 at 09:00
  • Right, so this is an argument FOR rewriting at the container level. Any reason why someone would rewrite it at the application level, like this link suggests ? http://stackoverflow.com/questions/950497/is-there-a-url-rewriting-engine-for-tomcat-java Is there any benefit in that ? – Andrei Oct 03 '12 at 09:01
  • I don't know UrlRewriteFilter but I assume than main advantage would be access to app login on rewrite phase. For example rewriting URL /users/username to /users/10 (apache doesn't have DB access to do such rewrites), but imo it's very bad practice. Other argument would be having all (app, rewrites) in one layer but once again - not a good practice. If something is better in one task like rewrites why forcing it one another layer? – Maciej Dragan Oct 03 '12 at 09:08
  • That's the whole point of the question... to find which way is better and when. I'm in the favor of applying it to container level. Also, I'm talking about root context rewrites rather than logins, etc. – Andrei Oct 03 '12 at 09:11