---
schema_version: "1"
id: 74a572dd152bc7523a998e24e43dbb4e4eecee998e71db5d1951cc549e9844be
title: UrlEncode()
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/marketing-cloud/ampscript/references/fun_http/mc-ampscript-reference-http-url-encode.md
canonical_url: https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-http/mc-ampscript-reference-http-url-encode.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 82be33ba3afd6f7c212d53d11bc5f9d3007d9146cbf35e2b2a984d548ba6a9b7
products:
  - AMPscript for Marketing Cloud
  - Marketing Cloud
---
# UrlEncode()

Modifies a string to only include characters that are safe to use in URLs.

**important:** This function treats all input strings as literal text. If you pass it an input string that includes a nested AMPscript expression, the function doesn’t evaluate the expression in the string. See [Handling Dynamic Values](#handling-dynamic-values).

## Availability

<table>
  <tr>
    <td>Marketing Cloud Engagement</td>
    <td>✅ Yes</td>
  </tr>
  <tr>
    <td>Marketing Cloud Next</td>
    <td>✅ Yes</td>
  </tr>
</table>

## Syntax

```text
URLEncode(urlToEncode, boolEncodeAllChars, boolEncodeAllStrings)
```

The `URLEncode()` function has these parameters:

- `urlToEncode` (string): Required. The string to convert to a format that is safe to include in URLs.
- `boolEncodeAllChars` (boolean): If `true`, the function converts all spaces and non-ASCII characters in a URL parameter string to their hexadecimal character codes. If `false`, the function only converts spaces in a URL parameter string to the hexadecimal character code `%20`, and leaves other characters unchanged. The default value is `false`.
- `boolEncodeAllStrings` (boolean): If `true`, the function converts any text string that you pass as the first parameter into a version that is safe to use in URLs. If `false`, the function only converts a string into a URL-safe version if the unsafe characters are part of a URL parameter string.

## Usage

To use this function, pass it a URL to encode. This example converts a URL that contains a parameter string that contains a space in the key name.

```text
%%[
  URLEncode('https://example.com?Last Name=野口')
]%%
```

The function returns a URL in which only space characters are converted into hexadecimal characters. It doesn’t convert the Unicode characters that are contained in the string.

```text
https://example.com?Last%20Name=野口
```

### Converting All Characters

You can control which characters the function converts by changing the value of the `boolEncodeAllChars` parameter. This example converts all non-ASCII characters to hexadecimal encodings.

```text
%%[
  URLEncode('https://example.com?Last Name=野口', true)
]%%
```

The function returns a string in which all the non-ASCII characters in the parameter string are converted to their hexadecimal equivalents.

```text
https://example.com?Last+Name%3d%e9%87%8e%e5%8f%a3
```

### Converting an Arbitrary String

You can convert a non-URL string into a URL-encoded string by changing the value of the `boolEncodeAllStrings` parameter. This example converts a string of text that isn’t a URL into a string that is safe to include in a URL.

```text
%%[
  URLEncode('Sample Text: サンプルテキスト', true, true)
]%%
```

The function returns a string in which all the non-ASCII characters and spaces are converted to a URL-safe format.

```text
Sample+Text%3a+%e3%82%b5%e3%83%b3%e3%83%97%e3%83%ab%e3%83%86%e3%82%ad%e3%82%b9%e3%83%88
```

### Handling Dynamic Values

The function converts all input strings to URL-safe strings. If your input string includes an AMPscript expression, the expression isn’t evaluated before being encoded. For this reason, always resolve dynamic values before encoding them.

For example, in this function, the value being provided to `UrlEncode()` contains an AMPscript expression within a string.

```text
%%=UrlEncode(Concat("/promo/", "%%=v(@segment)=%%"))=%%
```

As a result, this code doesn’t work as expected because the function converts the literal text `%%=v(@segment)=%%` to a URL-safe value.

However, if you store the expression as a variable, all the expressions nested in the variable are evaluated. If you then pass the variable to `UrlEncode()`, the function works as expected. This example shows how to implement this scenario by using variable expansion.

```text
%%[
  Var @url
  Set @url = Concat("/promo/", v(@segment))
]%%

%%=UrlEncode(@url)=%%
```

A `UrlEncode()` expression can include nested functions. For example, you can use `UrlEncode()` to encode the value of a string returned by an [HttpGet()](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/fun_http/mc-ampscript-reference-http-get.md) expression.

```text
%%[
  Var @couponCodeUrl
  Set @couponCodeUrl = Concat("https://example.com/getCode?id=", %%SubscriberId%%)
]%%

%%=UrlEncode(HttpGet(@couponCodeUrl))=%%
```

In this example, as long as the string that the `HttpGet()` function returns doesn’t have AMPscript code within it, the function behaves as expected.

Finally, you can pass subscriber attributes, such as data from an email subscriber profile or a sendable data extension. This example shows how to send the `FirstName` attribute to the `UrlEncode()` function.

```text
%%=UrlEncode(%%FirstName%%)=%%
```

**note:** Before July 2026, the `UrlEncode()` function expanded some functions embedded in strings. For security reasons, this type of expansion is no longer allowed. See _Knowledge Article_: [Updates to UrlEncode() AMPscript function](https://help.salesforce.com/s/articleView?id=005388674&type=1).
