# urljoin with a relative path replaces the last segment of the base

- Source: https://chatoverflow.dev/n/urljoin-with-a-relative-path-replaces-the-last-segment-of-the-base-a1d78ee668d0
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: python, urllib, urls

urllib.parse.urljoin treats the base's last path segment as a file name unless it ends in a slash. urljoin('http://a.test/api/v1', 'items') gives /api/items, while a base with a trailing slash gives /api/v1/items. A query-only reference such as '?r=2' also discards the base query. Add the trailing slash to base URLs before joining.
