URL Join¶
Zero-dependency Python snippets for joining URLs using the standard library.
6 snippets available in this sub-category.
Simple¶
Join base and relative URLs¶
url
join
base
relative
urljoin
web
Join base and relative URLs
from urllib.parse import urljoin
base = 'https://example.com/docs/'
relative = 'about/contact.html'
full_url = urljoin(base, relative)
print(full_url) # https://example.com/docs/about/contact.html
Notes
- urljoin resolves relative paths against a base URL
- Handles missing or extra slashes
Join with dot segments (.. and .)¶
url
join
dot
segments
urljoin
web
Join with dot segments
from urllib.parse import urljoin
base = 'https://example.com/docs/tutorials/'
relative = '../about/./contact.html'
full_url = urljoin(base, relative)
print(full_url) # https://example.com/about/contact.html
Notes
- .. moves up one directory, . stays in the same directory
Join with/without trailing slashes¶
url
join
trailing-slash
urljoin
web
Join with/without trailing slashes
from urllib.parse import urljoin
base1 = 'https://example.com/docs'
relative1 = 'page.html'
print(urljoin(base1, relative1)) # https://example.com/page.html
base2 = 'https://example.com/docs/'
relative2 = 'page.html'
print(urljoin(base2, relative2)) # https://example.com/docs/page.html
Notes
- Trailing slash on base affects the result
- No slash: replaces last segment; slash: appends to directory
Complex¶
Join with query and fragment¶
url
join
query
fragment
urljoin
web
Join with query and fragment
from urllib.parse import urljoin
base = 'https://example.com/search?q=python'
relative = 'results#section2'
full_url = urljoin(base, relative)
print(full_url) # https://example.com/results#section2
Notes
- Relative URL can override query and fragment of base
Join with absolute relative URL¶
url
join
absolute
urljoin
web
Join with absolute relative URL
from urllib.parse import urljoin
base = 'https://example.com/docs/'
relative = 'https://other.com/page'
full_url = urljoin(base, relative)
print(full_url) # https://other.com/page
Notes
- If relative is absolute, it replaces the base
Edge Cases¶
Join with empty base or malformed URLs¶
url
join
edge-case
empty
malformed
urljoin
web
Join with empty base or malformed URLs
from urllib.parse import urljoin
# Empty base
print(urljoin('', 'page.html')) # page.html
# Malformed base
print(urljoin('not-a-url', 'page.html')) # page.html
Notes
- If base is empty or malformed, returns the relative URL as-is
🔗 Cross-References¶
- Reference: See 📂 Build URL
- Reference: See 📂 Parse URL
- Reference: See 📂 URL Encode String
🏷️ Tags¶
url
, join
, base
, relative
, query
, fragment
, dot
, trailing-slash
, edge-case
, web
, urljoin
📝 Notes¶
- Use
urljoin
for robust URL joining and resolution - Always validate and normalize URLs before use
- For advanced manipulation, see
urllib.parse
docs