users@glassfish.java.net

Re: Global Redirects and default-web.xml

From: Jan Luehe <Jan.Luehe_at_Sun.COM>
Date: Fri, 19 Feb 2010 17:53:45 -0800

On 02/19/10 02:24 PM, glassfish_at_javadesktop.org wrote:
> Hi,
>
> I'm trying to do something which would seem rather simple and a common use case but I cannot find a good solution for this in Glassfish (v2.1).
>
> My scenario is simple. I want to redirect anyone coming into my site with just the domain, to be redirected to a full host name. e.g.:
>
> http://domain.com -> http://www.domain.com
>
> and also anyone coming into the site with another domain (because of 'legacy' domain names) being redirected to the my primary host name. e.g.:
>
> http://olddomain.com -> http://www.domain.com
> http://www.olddomain.com -> http://www.domain.com
>
> The main issue is that I want to do this for *all* possible urls on the site, no matter what the URL path and query parameters might be. e.g.:
>
> http://domain.com/foo/bar/mypage.jsp?id=5 -> http://www.domain.com/foo/bar/mypage.jsp?id=5
> etc.
>
> I've tried what's described here:
>
> http://www.sun.com/bigadmin/sundocs/articles/urlrdn.jsp
>
> But the redirect only happens for the exact matches. So the base root URL is redirected but not anything below it. We have many points of entry into the site and links to specific pages via search engines, etc. and need a global solution.
>
> I can rather easily do this in code as a filter, but the filter is web app specific. I've also seen this: http://tuckey.org/urlrewrite/ but again this would be web app context specific.
>
> I've been thinking about maybe trying to put a filter in default-web.xml but this seems to be loaded by each web app and I'm not sure how well that would behave...
>
> So my questions are:
>
> 1. Is there any out of the box way to do this? I would think there is a general handler in the Application Server which routes requests to each web application so it would be great if there was a way to bolt into it to handle this.
>
> 2. If there isn't an out of the box solution, would putting the UrlRewrite filter, or my own custom filter, in default-web.xml be recommended? It seems like having this load up with each web app in the application server is not great.
>
> BTW, front ending this with Apache and mod_rewrite is not an option for this scenario. Basically what I need is a mod_rewrite for Grizzly or something of the sort.
>
> Thanks,
>
> Andrea
>

Hi Andrea,

first of all, could you please upgrade to v2.1.1? :)

You were looking at the right docs, and the "redirect_n"
property of <virtual-server> is your best bet for what you are trying to
achieve,
but I'm afraid that it does not completely address your requirements.

For starters, you could add this property to the <virtual-server> with
id "server"
in your domain:

  <property name="redirect_1" value="from=
url-prefix=http://www.domain.com"/>

Note the empty "from" value.

This will cause any request targeted to the root context of any domain
served by your http-listener-1 to be redirected to
http://www.domain.com, as in:

  http://olddomain.com -> http://www.domain.com
  http://www.olddomain.com -> http://www.domain.com

which is what you want.

A "from" value of the form "/", as in

  <property name="redirect_1" value="from=/
url-prefix=http://www.domain.com"/>

is equivalent to "/*" and is supposed to match a URI subtree, which it does
under certain conditions, but it was causing redirects in an infinite
loop (because
"/" matches any (redirect) URI), so we had to find a way to break out
from the loop,
at the cost of making "/" less useful. I think we went a little too far
there.

I have a one-line fix available for v3.1 (and possibly v3.0.1) which
does exactly
what you are looking for, but given that you are on v2.x, it may not be
of too much
help to you (unless you have a support contract for GlassFish, in which
case the fix
could be backported to a v2.1.1 patch release).


Jan

> [Message sent by forum member 'arosso' (arosso_at_esri.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=387678
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>