Redirect non www version of domain to www in Jetty

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 05:28:32

问题


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?


回答1:


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>



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



回答3:


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>



回答4:


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.




回答5:


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>


来源:https://stackoverflow.com/questions/3539143/redirect-non-www-version-of-domain-to-www-in-jetty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!