views:

78

answers:

4

I cannot redirect my non www domain version to www with MovedContextHandler, it does not have host to redirect to.

Both www.myhost.com and myhost.com point to my web server IP. When someone tries to open myhost.com he is still able to access my site that way. I want for his browser to receive 301 to www.myhost.com instead. It is important for search rankings as search engines must know myhost.com and www.myhost.com are one and the same.

As a bonus, when someone tries to access myhost.com/somepath/somepage.html I want a 301 to www.myhost.com/somepath/somepage.html

How do I proceed with that? Do I need to write my own handler or is there an easier way? Thanks!

A: 

Two easy ways to do it:

  • if you have apache or any other front server in front of jetty you cat use mod_rewrite or whatever the front server have for this purpose,
  • if you'd rather want to have it done on jetty side I'd suggest you writing a Filter in your application (mapped to /* or whatever your servlet mapping is) which would do the redirection job. Such a filter should not be longer than a couple of lines.

IMHO filter solution is better than writing your own handler or tweaking jetty configuration because you would have much less work during jetty upgrades and production releases. You would have all you need inside your app - so no need to worry about the env during deployments.

Piotr Maj
Thanks pal, I am doing all without web app. As my content is mostly static and I don't want additional complexity involved. I want top speed from the server. Even the few servlets I use are registered derectly in the XML configuration. I was hoping to avoid writing filters.
avok00
A: 

I found the solution by looking at the source. You just need to specify the schema in the URL you are redirecting to inside the MovedContextHandler. Like this: http://www.somedomain.com If you only do www.somedomain.com, the redirect won't work properly.

This 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"&gt;

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

Can you post more info on what you did? The documentation is appalling.

Also, it is called a protocol, not a schema.

@Piotr Maj

I have filters setup in my app, but I want to do this correctly, at the domain handling level not the application level. I don't want to put domains in my app, why should I put domains in my app, why are you advocating tying my application to a domain? It isn't the right way to do something.

srslystfu
Regarding what it is called, you may educate yourself here ;) - http://en.wikipedia.org/wiki/URI_scheme
avok00
And about what I did, I updated my answer. GL!
avok00
A: 

This is exactly what I've done.

However, where do you put this? Just in /contexts - the name of the file is irrelevant? It is tied just via the contextPath setting?

I also tried with / without virtualhosts on my config. It never works, I've added it in /contexts and tried it in jetty.xml too

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

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

Yep on s/schema/scheme.

Thanks for the update. I've googled around and it would take a room of geniuses to painstakingly write as much documentation as exists that completely fails to accurately convey the information required to get this done.

srslystfu
The information is very unadequate I cannot agree more. And their user group support is nonexistent. Like most open source products, you must pay for support to get anything serious done. Or waste your own resources on it (that cost money too). One could think that the scarce information is on purpose. Nevertheless, just put this file in the /contexts and it should work. Yes, the name does not matter. Jetty automatically collects new files in this folder and parses them. If you did not disable that of course. The context will only handle the request if other context did not handle it. Debug it
avok00