Search code examples
url-rewritingjbosssingle-page-applicationbookmarksundertow

How do I configure Undertow handlers to support proper rewriting for SPA bookmarking?


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.

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')

Unfortunately, this doesn't seem to be rewriting anything. How can I update this configuration to property rewrite URLs?


Solution

  • As a start, try this configuration in WEB-INF/undertow-handlers.conf:

    path-prefix('/api') -> done
    path-suffix('.js') -> done
    path-prefix('/') -> rewrite('/')
    

    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.

    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.