Tomcat UrlRewriteFilter 部署项目虚拟路径配置,路由重写,可参考配置部署前端控制路由项目(Vue,React 等)
环境:
Windows 11
、apache-tomcat-8.5.78
1. 例:有以下 myapp
项目包:
/tomcat/
└── webapps/
└── myapp/
├── WEB-INF/
│ └── web.xml
├── index.html
└── my.html
<!-- 以下为 index.html 内容 -->
<!-- my.html 与 index.html 内容基本相同,仅为演示非index.html命名的文件也能被正常配置访问 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index</title>
<style>
html,
body,
h1 {
margin: 0;
}
html,
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
background: url(/static/img/bg.png) center no-repeat;
background-size: 100% 100%;
}
.h1 {
color: gold;
width: 80vw;
text-align: center;
margin: 0 auto;
}
.h1.white {
color: white;
margin-bottom: 30px;
}
</style>
</head>
<body>
<script>
document.write(`<h1 class="h1 white">index.html</h1><h1 class="h1">访问路径:${window.location.pathname}</h1>`)
</script>
</body>
</html>
<!-- 以下为 web.xml 内容 -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- SERVLET 启动初始化 -->
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
2. 启动Tomcat进行访问
- 访问:
http://localhost/myapp/
- 响应:index.html 对应资源
- 访问:
http://localhost/myapp/index.html
- 响应:index.html 对应资源
- 访问:
http://localhost/myapp/my.html
- 响应:my.html 对应资源
- 访问:
http://localhost/myapp/index
- 响应:404
3. 通过配置虚拟路径,移除项目路由前缀 /myapp
和 新增路由前缀 /myapp-custom
- 配置
tomcat/conf/server.xml
<!-- 找到server.xml中Host标签,并插入Context标签配置,如下 -->
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<!-- other code ... -->
<!-- 配置 / 路由 要映射到的项目目录 -->
<Context path="/" docBase="C:\tomcat\webapps\myapp" reloadable="false" ></Context >
<!-- 配置 /myapp-custom 路径 要映射到的项目目录,此处与 / 路由资源映射相同 -->
<Context path="/myapp-custom" docBase="C:\tomcat\webapps\myapp" reloadable="false" ></Context >
<!-- other code ... -->
</Host>
4. 重启 Tomcat 访问测试
- 访问:
http://localhost/
- 响应:index.html 对应资源
- 访问:
http://localhost/myapp/
- 响应:index.html 对应资源
- 访问:
http://localhost/my.html
- 响应:my.html 对应资源
- 访问:
http://localhost/myapp-custom/
- 响应:index.html 对应资源
- 访问:
http://localhost/myapp-custom/index.html
- 响应:index.html 对应资源
- 访问:
http://localhost/myapp-custom/my.html
- 响应:my.html 对应资源
5. 通过配置 Servlet
+ UrlRewriteFilter
自定义路由访问指定资源
- 配置
tomcat/webapps/myapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- SERVLET 启动初始化 --> <servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- 路由映射 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/myapp-web/*</url-pattern> </servlet-mapping> <!-- 应用 UrlRewriteFilter --> <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>logLevel</param-name> <param-value>WARN</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
- 配置
UrlRewriteFilter
,在tomcat/webapps/myapp/WEB-INF/
目录下新增配置文件urlrewrite.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN" "http://tuckey.org/res/dtds/urlrewrite2.6.dtd"> <!-- Configuration file for UrlRewriteFilter http://tuckey.org/urlrewrite/ --> <urlrewrite> <rule> <note> The rule means that requests to /test/status/ will be redirected to /rewrite-status the url will be rewritten. </note> <from>/test/status/</from> <to type="redirect">%{context-path}/rewrite-status</to> </rule> <outbound-rule> <note> The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url) the url /rewrite-status will be rewritten to /test/status/. The above rule and this outbound-rule means that end users should never see the url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks in your pages. </note> <from>/rewrite-status</from> <to>/test/status/</to> </outbound-rule> <!-- 配置将 /myapp-web 映射到资源 my.html --> <rule> <from>^/myapp-web$</from> <to>/my.html</to> </rule> </urlrewrite>
- 下载 UrlRewriteFilter, 官方下载
- 将下载的
urlrewrite-x.x.x.jar
文件复制到tomcat/lib/
目录下 - 最终目录结构
/tomcat/ ├── lib/ │ ├── ... other jar │ ├── urlrewrite-x.x.x.jar │ └── ... other jar └── webapps/ └── myapp/ ├── WEB-INF/ │ ├── urlrewrite.xml │ └── web.xml ├── index.html └── my.html
- UrlRewriteFilter 官网
6. 重启 Tomcat 访问测试
- 访问:
http://localhost/
- 响应:index.html 对应资源
- 访问:
http://localhost/myapp/
- 响应:index.html 对应资源
- 访问:
http://localhost/my.html
- 响应:my.html 对应资源
- 访问:
http://localhost/myapp-custom/
- 响应:index.html 对应资源
- 访问:
http://localhost/myapp-custom/index.html
- 响应:index.html 对应资源
- 访问:
http://localhost/myapp-custom/my.html
- 响应:my.html 对应资源
- 访问:
http://localhost:8888/myapp-web
- 响应:my.html 对应资源
- 访问:
http://localhost/myapp-custom/myapp-web
- 响应:my.html 对应资源
- 访问:
http://localhost/myapp-custom/myapp-web/my.html
- 响应:404