You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Open Liberty中通过server.xml实现上下文根重定向或多上下文根方案咨询

Open Liberty 上下文根修改与重定向/多根配置方案

方式一:修改上下文根并添加永久重定向(推荐)

这种方式将应用的上下文根改为/newroot,同时对旧路径/test的请求做301永久重定向,确保新旧路径都能正常访问,还能引导搜索引擎更新索引。

  1. 在server.xml中启用rewrite-1.0特性:
<featureManager>
  <!-- 保留你已启用的其他特性 -->
  <feature>rewrite-1.0</feature>
</featureManager>
  1. 更新application标签的上下文根为/newroot:
<application id="demo" location="demo-0.0.3-SNAPSHOT.war" name="demo" context-root="/newroot"></application>
  1. 添加重写规则实现路径重定向:
<rewrite id="rewriteTestToNewRoot">
  <rule match="/test(.*)" url="/newroot$1">
    <redirect type="permanent"/>
  </rule>
  <rule match="/test" url="/newroot">
    <redirect type="permanent"/>
  </rule>
</rewrite>

方式二:为同一应用设置两个上下文根

如果需要两个路径直接指向应用(无重定向跳转),有两种实现方式:

子方式A:部署同一应用两次(多实例)

通过添加两个application标签,指向同一个WAR包,分别设置不同的上下文根和唯一的id、name:

<application id="demo-test" location="demo-0.0.3-SNAPSHOT.war" name="demo-test" context-root="/test"></application>
<application id="demo-newroot" location="demo-0.0.3-SNAPSHOT.war" name="demo-newroot" context-root="/newroot"></application>

注意:这种方式会启动两个独立的应用实例,内存占用翻倍,仅适合特殊场景使用。

子方式B:通过重写实现单实例多上下文根(推荐)

利用rewrite-1.0特性,将/test的请求内部转发到/newroot,无需启动多个实例:

  1. 启用rewrite-1.0特性(同方式一)
  2. 设置应用上下文根为/newroot(同方式一)
  3. 添加内部转发规则:
<rewrite id="rewriteBothRoots">
  <rule match="/test(.*)" url="/newroot$1">
    <forward/>
  </rule>
  <rule match="/test" url="/newroot">
    <forward/>
  </rule>
</rewrite>

这样用户访问my.host/test或my.host/newroot都会指向同一个应用实例,浏览器地址栏不会发生变化。


内容的提问来源于stack exchange,提问作者xcs4me

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 14:17:50