#12878·wagtail

URL generation in richtext pages link handler incorrectly handles multi-site setups

Author: joeyjurjensCreated Feb 13, 2025Updated Sep 12, 2026
Labelstype:Bugstatus:Unconfirmed

Issue Summary

When using richtext fields with page links in a multi-site setup, the URLs are generated without proper site context, leading to potential incorrect URLs. This happens because the page link handler doesn't have access to the current request/site when generating URLs.

Steps to Reproduce

  1. Create a multi-site Wagtail setup with two sites (127.0.0.1, port 8000 & 127.0.0.1 port 9000)
  2. Translate the root page of site 1 to a different locale, and set it this page as root for site 2.
  3. Run two servers (with manage.py), and use different settings with a different language code for both.
  4. Visit a page with a page link within a richtext field on both port 8000 & 9000
  5. One of these pages will actually link to the other site's url.
  • I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes

Technical details

The problem lays in the fact that richtext page links have no context of the current request or even site when it's being rendered. This means, that the get_url is called with no request or site passed in.

Then get_url_parts will return the wrong parts, as without request, it's just guessing;

get_url will then use the root_url that get_url_parts returns in the generation of the url when there's no current_site; return root_url + page_path

I think this part might be the issue and is actually incorrect. When we have page instance, and no current site available, shouldn't we really just return the page_path only?

Also, the get_site_root_paths static method on Site, shouldn't this first return the site that matches the current language? Though we might need different cache key in this scenario.

But honestly, the real problem seems to be the richtext template filter.

Proposed Solution(s)

  1. Change the behaviour of get_url OR PageLinkHandler

Implementation would involve:

  • Don't return root_url + page_path in get_url if we have not found the precise site. OR
  • Don't use .url in PageLinkHandler but rather use something that only returns the path?

  1. Use asgiref.Local (similar to Wagtail's logging system) to maintain request/site context during page serving and template rendering. This would allow the link handler to access the current site when generating URLs.

Implementation would involve:

  1. Add request/site context handling using asgiref.Local
  2. Add this context within the serve view
  3. Update the page link handler to use the context for URL generation (and maybe other places)

Working on this

I can work on this if one of the proposals sound good, or there are other proposals from you guys.

This should then also fix; https://github.com/wagtail/wagtail/issues/5876