I am unable to redirect my non www domain version to www with MovedContextHandler, it doesn't have host to redirect to.

Both www.myhost.com and myhost.com indicate my web server IP. When someone attempts to open myhost.com he's still in a position to access my website this way. I would like for his browser to get 301 to www.myhost.com rather. It is necessary for search ratings as search engines like google have to know myhost.com and www.myhost.com is one and also the same.

Like a bonus, when someone attempts to access myhost.com/somepath/somepage.html I would like a 301 to www.myhost.com/somepath/somepage.html

How do you proceed with this? Should i write my very own handler or perhaps is there an simpler way? Thanks!

To prevent a cycle of redirections you need to define on which virtualhost this rule works.

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.server.handler.MovedContextHandler">
  <Set name="contextPath">/</Set>
  <Set name="newContextURL">http://www.example.com</Set>
  <Set name="permanent">true</Set>
  <Set name="discardPathInfo">false</Set>
  <Set name="discardQuery">false</Set>

  <Set name="virtualHosts">
    <Array type="String">
          <Item>example.com</Item>
    </Array>
  </Set>

</Configure>

Two easy ways to get it done:

  • for those who have apache or other front server before jetty you cat use mod_rewrite or regardless of the front server have for this function,
  • if you would rather wish to have it done on jetty side I'd suggest you writing a Filter inside your application (planned to /* or whatever your servlet mapping is) which may perform the redirection job. This type of filter shouldn't be more than a few lines.

IMHO filter solution is preferable to writing your personal handler or fine-tuning jetty configuration since you might have a smaller amount work throughout jetty upgrades and production releases. You'd have all that's necessary within your application - so you don't need to be worried about the env throughout deployments.

I discovered the answer by searching in the source. You need to simply specify the schema within the URL you're redirecting to within the MovedContextHandler. Such as this: http://www.somedomain.com Should you only do www.somedomain.com, the redirect will not work correctly.

This really is my redirector.xml

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.server.handler.MovedContextHandler">
  <Set name="contextPath">/</Set>
  <Set name="newContextURL">http://www.somedomain.com</Set>
  <Set name="permanent">true</Set>
  <Set name="discardPathInfo">false</Set>
  <Set name="discardQuery">false</Set>
</Configure>

Are you able to publish more information on which you probably did? The documentation is appalling.

Also, it's known as a protocol, not really a schema.

@Piotr Maj

I've filters setup during my application, however i do properly, in the domain handling level not the applying level. I'd rather not put domain names during my application, why must I put domain names during my application, the reason for promoting tying my application to some domain? It is not the proper way to make a move.