Skip to main content

url-normalize

CI PyPI

URI Normalization function:

  • Take care of IDN domains.
  • Always provide the URI scheme in lowercase characters.
  • Always provide the host, if any, in lowercase characters.
  • Only perform percent-encoding where it is essential.
  • Always use uppercase A-through-F characters when percent-encoding.
  • Prevent dot-segments appearing in non-relative URI paths.
  • For schemes that define a default authority, use an empty authority if the default is desired.
  • For schemes that define an empty path to be equivalent to a path of "/", use "/".
  • For schemes that define a port, use an empty port if the default is desired
  • All portions of the URI must be utf-8 encoded NFC from Unicode strings

Inspired by Sam Ruby's urlnorm.py

Features

  • IDN (Internationalized Domain Name) support
  • Configurable default scheme (https by default)
  • Configurable default domain for absolute paths
  • Query parameter filtering with allowlists
  • Support for various URL formats including:
    • Empty string URLs
    • Double slash URLs (//domain.tld)
    • Shebang (#!) URLs
  • Cross-version Python compatibility (3.8+)
  • 100% test coverage
  • Modern type hints and string handling

Installation

pip install url-normalize

Usage

from url_normalize import url_normalize

# Basic normalization (uses https by default)
print(url_normalize("www.foo.com:80/foo"))
# Output: https://www.foo.com/foo

# With custom default scheme
print(url_normalize("www.foo.com/foo", default_scheme="http"))
# Output: http://www.foo.com/foo

# With query parameter filtering enabled
print(url_normalize("www.google.com/search?q=test&utm_source=test", filter_params=True))
# Output: https://www.google.com/search?q=test

# With custom parameter allowlist as a dict
print(url_normalize(
    "example.com?page=1&id=123&ref=test",
    filter_params=True,
    param_allowlist={"example.com": ["page", "id"]}
))
# Output: https://example.com?page=1&id=123

# With custom parameter allowlist as a list
print(url_normalize(
    "example.com?page=1&id=123&ref=test",
    filter_params=True,
    param_allowlist=["page", "id"]
))
# Output: https://example.com?page=1&id=123

# With default domain for absolute paths
print(url_normalize("/images/logo.png", default_domain="example.com"))
# Output: https://example.com/images/logo.png

# With default domain and custom scheme
print(url_normalize("/images/logo.png", default_scheme="http", default_domain="example.com"))
# Output: http://example.com/images/logo.png

Command-line usage

You can also use url-normalize from the command line:

$ url-normalize "www.foo.com:80/foo"
# Output: https://www.foo.com/foo

# With custom default scheme
$ url-normalize -s http "www.foo.com/foo"
# Output: http://www.foo.com/foo

# With query parameter filtering
$ url-normalize -f "www.google.com/search?q=test&utm_source=test"
# Output: https://www.google.com/search?q=test

# With custom allowlist
$ url-normalize -f -p page,id "example.com?page=1&id=123&ref=test"
# Output: https://example.com/?page=1&id=123

# With default domain for absolute paths
$ url-normalize -d example.com "/images/logo.png"
# Output: https://example.com/images/logo.png

# With default domain and custom scheme
$ url-normalize -d example.com -s http "/images/logo.png"
# Output: http://example.com/images/logo.png

# Via uv tool/uvx
$ uvx url-normalize www.foo.com:80/foo
# Output: https://www.foo.com:80/foo

Documentation

For a complete history of changes, see CHANGELOG.md.

License

MIT License

Metadata

Release files for url-normalize 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for url-normalize 2.2.0
File Size Uploaded Provenance Actions
url_normalize-2.2.0.tar.gz 18.2 kB Without provenance Details for url_normalize-2.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for url-normalize 2.2.0
File Interpreter ABI Platform Actions
url_normalize-2.2.0-py3-none-any.whl Python 3 none any Details for url_normalize-2.2.0-py3-none-any.whl

Total release size: 32.4 kB

Release files / url_normalize-2.2.0.tar.gz

Download URL url_normalize-2.2.0.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0f0b7cc95a95d2d9b0c9a51d47a326559bc05bd1558accdada21bb0c9504de85
BLAKE2b-256 checksum
How to use checksums
1c94d79b960d66b933492f7dfbec8db799fa7033e7fbc98090f46fa581ca3a94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.11

Release files / url_normalize-2.2.0-py3-none-any.whl

Download URL url_normalize-2.2.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3fe387b62f5b66db94304bc703bf6d34de52aaa9590d4d1f1bbdf305a1430064
BLAKE2b-256 checksum
How to use checksums
f9ce73b35517cf49836ed8c7ca74c7da7808feca367788986f38e5e63f93e6cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.11

Security for url-normalize 2.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page