Skip to content

Fix custom base URL with subpath causing 404 errors#3161

Open
chrisuthe wants to merge 3 commits intomusic-assistant:devfrom
chrisuthe:bug/custom-base-url
Open

Fix custom base URL with subpath causing 404 errors#3161
chrisuthe wants to merge 3 commits intomusic-assistant:devfrom
chrisuthe:bug/custom-base-url

Conversation

@chrisuthe
Copy link
Contributor

When base_url contains a subpath (e.g., https://link.com/music), extract the path component and prefix all route registrations so the server works correctly behind a reverse proxy with path-based routing. Also redirect bare root to the subpath and mark base_url config as requires_reload since routes are registered at startup.

Fixes music-assistant/support#4940

Tested on HAOS With DEV Plugin
image

This is a small amount of code with GIANT testing implications

@chrisuthe chrisuthe marked this pull request as draft February 13, 2026 18:15
@chrisuthe
Copy link
Contributor Author

Updated the PR to handle both ingress and reverse proxy. I have tested with both but that does not cover all possible reverse proxy configurations.

@chrisuthe chrisuthe marked this pull request as ready for review February 15, 2026 18:28
"""Redirect root path to the base path when a subpath is configured."""
raise web.HTTPFound(self._base_path + "/")

async def _handle_prefixed_catch_all(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function introduces quite a bit of duplicate code. Could you refactor this into a function that can be used by both _handle_prefixed_catch_all and _handle_catch_all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created _resolve_dynamic_route taking request and lookup path then made _handle_prefixed and _handle_catch_all both thin wrappers.

Copy link
Contributor

@MarvinSchenkel MarvinSchenkel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since, as you mentioned, this as quite some test implications, I would love to see some unit tests for this that handle:

  • Current use cases (correct, should stay correct)
  • Current failing use cases (fail without your fix, pass with your fix applied)

Marking the PR as draft so we can keep track of things that need our attention. Please mark it as ready for review again when you want us to have another look 🙏

@MarvinSchenkel MarvinSchenkel marked this pull request as draft February 16, 2026 09:25
@chrisuthe
Copy link
Contributor Author

Since, as you mentioned, this as quite some test implications, I would love to see some unit tests for this that handle:

  • Current use cases (correct, should stay correct)
  • Current failing use cases (fail without your fix, pass with your fix applied)

Marking the PR as draft so we can keep track of things that need our attention. Please mark it as ready for review again when you want us to have another look 🙏

I added a suite of tests but be warned they are generated by AI. They all make sense to me who didn't know exactly where to start on unit tests for these changes. LMK If they missed the mark.

When base_url contains a subpath (e.g. https://link.com/music), register
all routes at BOTH root level and the prefixed path. Root-level routes
serve HA ingress (which strips the slug prefix) and direct access, while
prefixed routes serve requests from the reverse proxy. Dynamic routes are
stored without the prefix; a dedicated prefixed catch-all handler strips
the base_path before looking up dynamic route handlers.

Also marks base_url config as requires_reload since changing the subpath
requires re-registering all routes.

Fixes: music-assistant/support#4940
Extract _resolve_dynamic_route() to consolidate the identical
route-matching logic from _handle_prefixed_catch_all and
_handle_catch_all into a single method.
Cover dynamic route matching, prefix stripping, wildcard routes,
registration/unregistration, redirect behavior, and edge cases
for both standard and reverse-proxy-with-subpath configurations.
@chrisuthe chrisuthe marked this pull request as ready for review February 17, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subpath not removed when accessing with custom base url

3 participants