有没有可能在Tomcat5.5独立运行的情况下进行301重定向,而不是在IIS/Apache后面?
发布于 2010-09-22 22:48:59
没有一种方法可以像使用Apache那样简单地设置它。最接近的方法是创建一个servlet或jsp来处理重定向,然后将其映射到您想要重定向的URL。在servlet或jsp中,它会这样做:
response.setStatus(301);
response.setHeader("Location", "http://www.example.com/redirect-to-here.html" );发布于 2018-02-23 22:20:08
要将整个文件夹重定向到新位置,您需要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>
→ <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> https://stackoverflow.com/questions/3761997
复制相似问题