5

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

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

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

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

user11153
  • 8,536
  • 5
  • 47
  • 50
Boris Hamanov
  • 3,085
  • 9
  • 35
  • 58

5 Answers5

7

To avoid a cycle of redirections you have to define on what 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>
Bauna
  • 171
  • 1
  • 3
  • The idea of this rule is to serve all the different virtual host name variants not covered by other rules. What is the purpose of defining all of them explicitly? What redirections will this save exactly? – Boris Hamanov Nov 12 '10 at 10:21
  • fyi: in jetty 9 this should go in the webapps/ directory – rodney757 Jun 21 '16 at 22:24
2

I just want to write my own answer for those who are using embedded jetty:

MovedContextHandler rewriteHandler = new MovedContextHandler();
rewriteHandler.setContextPath("/");
rewriteHandler.setNewContextURL("http://www.example.com");
rewriteHandler.setPermanent(true);
rewriteHandler.setDiscardPathInfo(false);
rewriteHandler.setDiscardQuery(false);
rewriteHandler.setVirtualHosts(new String[] {"example.com"});
fen1ksss
  • 1,100
  • 5
  • 21
  • 44
1

You can do it using custom servlet filter:

DomainRedirectionFilter.java

public class DomainRedirectionFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String requestURL = httpRequest.getRequestURL().toString();
        URL url = new URL(requestURL);

        if (!url.getHost().startsWith("www.")) {
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            httpServletResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            httpServletResponse.setHeader("Location", requestURL.replace("://", "://www."));
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
    }
}

web.xml

<filter>
    <filter-name>DomainRedirectionFilter</filter-name>
    <filter-class>com.invenline.orgamer.web.servletFilter.DomainRedirectionFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>DomainRedirectionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
user11153
  • 8,536
  • 5
  • 47
  • 50
0

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
  • 117
  • 3
  • 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. – Boris Hamanov Aug 25 '10 at 08:33
  • See [my answer](http://stackoverflow.com/a/23736958) for servlet filter solution. – user11153 May 19 '14 at 11:48
0

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">

<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>
Boris Hamanov
  • 3,085
  • 9
  • 35
  • 58