问题描述
我正在尝试配置 JBoss EAP 7(通过 Undertow)以使用 Undertow 处理程序正确地将任何 SPA URL 重写回 SPA 的 index.html
.不幸的是,我的 API 位于 /api
,所以我需要让任何以 /api
开头的请求通过.
I am trying to configure JBoss EAP 7 (via Undertow) to properly rewrite any SPA URLS back to the SPA's index.html
using Undertow handlers. Unfortunately, my API is located at /api
, so I need to let any requests pass through which start with /api
.
这是我当前的配置(从另一个 SO 答案中提取):
Here is my current configuration (lifted from another SO answer):
not equals(%R, '/my-app') and
not equals(%R, '/my-app/') and
not equals(%R, '/my-app/index.html') and
not path-prefix('/my-app/api') and
not regex('/my-app/.*.js') and
regex('/my-app/.+') -> rewrite('/my-app/index.html')
不幸的是,这似乎并没有重写任何东西.如何更新此配置以重写 URL?
Unfortunately, this doesn't seem to be rewriting anything. How can I update this configuration to property rewrite URLs?
推荐答案
首先,在 WEB-INF/undertow-handlers.conf
中尝试这个配置:
As a start, try this configuration in WEB-INF/undertow-handlers.conf
:
path-prefix('/api') -> done
path-suffix('.js') -> done
path-prefix('/') -> rewrite('/')
您不应该在任何规则上使用 /my-app
前缀,因为它们已经在您的应用上下文中运行.
You shouldn't need the /my-app
prefix on any rules as they are already running in the context of your app.
但是,您可能需要添加其他谓词以防止重写其他资源,如样式表、网站图标、源映射等.谓词和处理程序的完整列表 有助于生成更具体、更有针对性的规则.
However, you may need to add other predicates to prevent rewriting other resources like stylesheets, favicons, sourcemaps, etc. The full list of predicates and handlers can be helpful to produce more specific, targeted rules.
请注意,path-suffix
仍然占像 /app?thing.js
这样的路径.尽管您可能永远不会使用这样的查询参数,但最好记住它会被重写.
Please note, path-suffix
still accounts for a path like /app?thing.js
. Though you may never use a query parameter like that, it's good to keep in mind that it'll be rewritten.
这篇关于如何配置 Undertow 处理程序以支持正确重写 SPA 书签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!