开发者

Redirect non www version of domain to www in Jetty

开发者 https://www.devze.com 2023-01-12 08:27 出处:网络
I cannot redirect my non www domain version to www with MovedContextHandler, it does not have host to redirect to.

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 w开发者_运维百科eb 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?


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>


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"});


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>


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消