UrlEncode()

Modifies a string to only include characters that are safe to use in URLs.

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.

Marketing Cloud Engagement ✅ Yes
Marketing Cloud Next ❌ No

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.

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.

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.

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.

The function returns a string in which all the non-ASCII characters in the parameter string are converted to their hexadecimal equivalents.

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.

The function returns a string in which all the non-ASCII characters and spaces are converted to a URL-safe format.

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.

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.

A UrlEncode() expression can include nested functions. For example, you can use UrlEncode() to encode the value of a string returned by an HttpGet() expression.

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.

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.