【问题标题】:Redirect non www version of domain to www in Jetty将非 www 版本的域重定向到 Jetty 中的 www
【发布时间】:2011-04-02 03:13:57
【问题描述】:

我无法使用MovedContextHandler 将我的非 www 域版本重定向到 www,它没有要重定向到的主机。

www.example.comexample.com 都指向我的 Web 服务器 IP。当有人试图打开example.com 时,他仍然可以通过这种方式访问​​我的网站。我想让他的浏览器接收 HTTP 301 重定向到www.example.com。这对搜索排名很重要,因为搜索引擎必须知道example.comwww.example.com 是一回事。

作为奖励,当有人尝试访问 example.com/somepath/somepage.html 时,我希望将 HTTP 301 重定向到 www.example.com/somepath/somepage.html

我该如何进行呢?我需要编写自己的处理程序还是有更简单的方法?

【问题讨论】:

    标签: redirect dns jetty


    【解决方案1】:

    为避免重定向循环,您必须定义此规则适用于哪个虚拟主机。

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

    【讨论】:

    • 此规则的想法是服务于其他规则未涵盖的所有不同虚拟主机名变体。明确定义所有这些的目的是什么?这将准确保存哪些重定向?
    • fyi:在 jetty 9 这应该放在 webapps/ 目录中
    【解决方案2】:

    我只是想为使用嵌入式码头的人写下我自己的答案:

    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】:

      您可以使用自定义 servlet 过滤器来做到这一点:

      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】:

        两种简单的方法:

        • 如果您在码头前面有 apache 或任何其他前端服务器,您可以使用 mod_rewrite 或任何前端服务器为此目的提供的任何东西,
        • 如果您希望在码头完成它,我建议您在应用程序中编写一个过滤器(映射到 /* 或任何您的 servlet 映射),它可以完成重定向工作。这样的过滤器不应超过几行。

        恕我直言,过滤器解决方案比编写自己的处理程序或调整码头配置要好,因为在码头升级和生产版本期间您的工作量会少得多。您将在应用程序中拥有所需的一切 - 因此在部署期间无需担心环境。

        【讨论】:

        • 谢谢朋友,我在没有网络应用程序的情况下做所有事情。因为我的内容大部分是静态的,我不希望涉及额外的复杂性。我想要服务器的最高速度。甚至我使用的少数 servlet 也直接在 XML 配置中注册。我希望避免编写过滤器。
        • 请参阅my answer 了解 servlet 过滤器解决方案。
        【解决方案5】:

        我通过查看源找到了解决方案。您只需要在 MovedContextHandler 中重定向到的 URL 中指定架构。像这样:http://www.somedomain.com 如果你只做 www.somedomain.com,重定向将无法正常工作。

        这是我的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>
        

        【讨论】:

          猜你喜欢
          • 2020-12-31
          • 2014-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-07
          • 1970-01-01
          相关资源
          最近更新 更多