I want to redirect all requests for http to https using Jetty (6.1.24). For some reason (my ignorance) this is eluding me. This is what I have:
<New id="redirect" class="org.mortbay.jetty.handler.rewrite.RedirectPatternRule">
<Set name="pattern">http://foobar.com/*</Set>
<Set name="location">https://foobar.com</Set>
</New>
In response I get 200 - ok, and the body is the page over http, ie the redirect doesn't occur.
Speaking for Jetty 9... Here's how you can do it provided that your SSL connector already works:
Step 1: Make sure everything goes through SSL by adding this to your web.xml. If you try to access a resource through HTTP, this will return a 403 !SECURE error
Step 2: Have Jetty redirect to HTTPS when it sees a 403 !SECURE error by adding this to your jetty.xml
I think that the pattern is matching only the URI. You should use something like:
See: http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/rewrite/handler/RewriteHandler.html
I just added the doc: http://wiki.eclipse.org/Jetty/Howto/Configure_SSL#Redirecting_http_requests_to_https
As far as I can tell, this is not easy to do with any of the rules/handlers that are shipped with Jetty 6.
The
RedirectPatternRule
matches on thetarget
which is the path in the Jetty server, and not the full URI, so your rule is never matching.You could change it to:
However, that has 2 issues:
https
requests)location
as it is specified, and ignores anything that was matched by thepattern
)You can overcome the first issue with some trickery.
You can wrap the
RewriteHandler
in aContextHandler
, and a context handler allows you to specify which connectors it will handle requests from (setConnectorNames
). So, you could use that to make the rewrite only apply to requests on the http connector(s).I can't think of a way to overcome the second issue though.
I think your best bet will to write your own redirect rule for this. If you don't have development resources to do that for you, then contact me (you can find my email address via my blog, which is in my profile) and I can whip one up (under the same license as Jetty). It will be pretty straight forward to write a rule that simply redirects http to https.