encode_uri

Encodes a URI by replacing special characters with their UTF-8 percent-encoded equivalents. Preserves characters that are valid in URIs (like :, /, ?, #, &, =).

{% assign url = "https://example.com/search?q=hello world&category=shoes" %}
{% log url | encode_uri %}

Output:

https://example.com/search?q=hello%20world&category=shoes

Syntax

{{ url | encode_uri }}
{{ string | encode_uri }}
Parameter
Description

url

The URL or string to encode

Characters Preserved vs Encoded

Preserved (not encoded)
Encoded

A-Z a-z 0-9

Spaces → %20

- _ . ! ~ * ' ( )

"%22

; , / ? : @ & = + $ #

< >%3C %3E

{ }%7B %7D

Non-ASCII → %XX

Examples

Encode URL with spaces:

{% assign product_name = "Blue Running Shoes" %}
{% assign search_url = "https://store.com/search?q=" | append: product_name | encode_uri %}

{% log search_url %}

Output:

Build API URL with parameters:

Encode redirect URL:

Handle international characters:

Output:

Encode tracking URL:

Build webhook callback URL:

Encode file path in URL:

Output:

Create mailto link:

encode_uri vs url_encode

Filter
Use Case
Encodes

encode_uri

Full URLs

Preserves : / ? # & =

url_encode

Query parameter values

Encodes everything including & =

Example difference:

When to use which:

Common Use Cases

Use Case
Example

Search URLs

`"/search?q="

Redirect URLs

Encode return URLs for OAuth flows

API requests

Encode URLs with dynamic parameters

Tracking links

Encode customer/order data in URLs

File downloads

Encode file names with spaces/special chars

Notes

  • Uses JavaScript's native encodeURI() function

  • Preserves URI structure characters (:, /, ?, #, &, =, etc.)

  • For encoding individual query parameter values, consider url_encode instead

  • Handles UTF-8 characters (international text) correctly

  • Safe to call multiple times (already-encoded characters won't be double-encoded)

  • See also: url_encode for encoding query parameter values

Last updated