| single |
# url-normalize
[tests]
[Coveralls]
[PyPI]
[Python Versions]
[License]
[Ruff]
Normalize URL spelling consistently for storage, comparison, and display.
Use `url_normalize()` for normalized URLs and `url_humanize()` for readable
display URLs.
Requires **Python 3.11 or newer**. Normalization operates on URL text; it
does not fetch pages or follow redirects.
## Installation
These examples use url-normalize 4.0.0. If you store normalized URLs or use
custom parameter filters, read the [4.0.0 upgrade notes] before upgrading.
The [3.1.0 usage guide] covers the previous release.
Install the library and CLI:
```sh
python -m pip install "url-normalize>=4.0.0"
```
Or install the CLI in its own environment with [uv]:
```sh
uv tool install "url-normalize>=4.0.0"
```
## Quick start
```python
from url_normalize import url_normalize
print(url_normalize("HTTP://Example.COM:80/a/../b"))
# Output: http://example.com/b
print(url_normalize("bücher.de/résumé"))
# Output: https://xn--bcher-kva.de/r%C3%A9sum%C3%A9
```
From the terminal:
```sh
url-normalize "HTTP://Example.COM:80/a/../b"
# Output: http://example.com/b
```
## Normalization behavior
| Behavior | Input | Output |
| --- | --- | --- |
| Add a missing scheme | `example.com` | `https://example.com/` |
| Lowercase scheme and host; remove a default port |
`HTTP://Example.COM:80/` | `http://example.com/` |
| Resolve dot segments | `https://example.com/a/../b` |
`https://example.com/b` |
| Collapse repeated path slashes | `https://example.com/a//b///` |
`https://example.com/a/b/` |
| Encode Unicode hosts and paths | `https://bücher.de/résumé` |
`https://xn--bcher-kva.de/r%C3%A9sum%C3%A9` |
| Remove one trailing host dot, including Unicode equivalents |
`https://example.com。/` | `https://example.com/` |
| Preserve encoded path separators; uppercase escape hex |
`https://example.com/a%2fb` | `https://example.com/a%2Fb` |
| Preserve query order and repeated keys |
`https://example.com/?b=2&a=1&a=3` | `https://example.com/?b=2&a=1&a=3` |
| Preserve explicit empty query values | `https://example.com/?flag=&flag`
| `https://example.com/?flag=&flag` |
| Remove empty query and fragment components | `https://example.com/?#` |
`https://example.com/` |
| Preserve an explicit relative reference | `../images/a.png` |
`../images/a.png` |
| Preserve fragment routes | `https://example.com/#!/users/42` |
`https://example.com/#!/users/42` |
| Preserve text-highlight syntax |
`https://example.com/#:~:text=hello%20world` |
`https://example.com/#:~:text=hello%20world` |
In query parameters, `+`, `%2B`, and `%20` remain distinct. Parameter
filtering is disabled by default. Fragments are preserved and normalized by
default, including hashbang routes (`#!/route`) and text directives
(`#:~:text=hello`). Use `strip_fragment=True` to remove the entire fragment
explicitly.
Fragments keep literal RFC 3986 fragment punctuation and existing ASCII
escapes, with uppercase escape hex. This deliberately includes escaped
letters, hyphens and tildes: fragment syntax can be interpreted before
percent-decoding. For example, `#:%7E:text=hello` stays encoded instead of
becoming an active text directive. Unicode text uses UTF-8 and NFC
normalization as usual. Hashbang fragments are no longer converted into
`_escaped_fragment_` query parameters.
Empty query and fragment delimiters are removed by default. Set
`keep_empty_components=True` in either Python API, or use
|