【问题标题】:permanent 301 redirect in Tomcat 5.5Tomcat 5.5 中的永久 301 重定向
【发布时间】:2010-09-21 15:45:29
【问题描述】:

是否可以在 Tomcat 5.5 独立运行而不是在 IIS/Apache 之后进行 301 重定向?

【问题讨论】:

    标签: http tomcat


    【解决方案1】:

    没有一种方法可以像使用 Apache 那样轻松地进行设置。最接近的方法是制作一个 servlet 或 jsp 来处理重定向,然后将其映射到您要从中重定向的 URL。在 servlet 或 jsp 中,它会执行以下操作:

    response.setStatus(301);
    response.setHeader("Location", "http://www.example.com/redirect-to-here.html" );
    

    【讨论】:

      【解决方案2】:

      要将整个文件夹重定向到新位置,您需要 JSP 和配置才能在 404 上调用此 jsp。

      index.jsp 中,您需要修改重定向行为。下面的代码将从OldApp 文件夹重定向到同一服务器上的NewApp 文件夹。

      index.jsp:

      <!DOCTYPE html>
      <html lang="en-US">
      <head>
          <meta charset="UTF-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          <title>301 Moved</title>
      </head>
      <%@ page import="org.apache.catalina.util.RequestUtil" %>
      <%
          // get the requested URI
          //String requestedLocation = request.getRequestURI();
          // original request
          String requestedLocation = RequestUtil.filter((String) request.getAttribute("javax.servlet.error.request_uri"));
          // rewrite to new location
          String newLocation = requestedLocation.replaceAll("^/OldApp", "/NewApp");
          // add query string
          String query = request.getQueryString();
          if (!query.isEmpty()) {
              newLocation = newLocation + '?' + query;
          }
      
          // 301 - permanent redirect
          response.setStatus(response.SC_MOVED_PERMANENTLY);
          response.setHeader("Location", newLocation);
      %>
      <body>
          &rarr; <a href="<%=newLocation%>"><%=newLocation%></a>
      </body>
      </html>
      

      WEB-INF/web.xml:

      <?xml version="1.0" encoding="ISO-8859-1"?>
      <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0"
        metadata-complete="true">
      
        <error-page>
          <error-code>404</error-code>
          <location>/index.jsp</location>
        </error-page>
      </web-app> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-24
        • 2013-07-17
        • 1970-01-01
        • 2012-05-31
        • 2015-08-11
        • 1970-01-01
        • 2014-01-12
        相关资源
        最近更新 更多