4.7.x common.inc drupal_urlencode($text)
5.x common.inc drupal_urlencode($text)
6.x common.inc drupal_urlencode($text)

Wrapper around urlencode() which avoids Apache quirks.

Should be used when placing arbitrary data in an URL. Note that Drupal paths are urlencoded() when passed through url() and do not require urlencoding() of individual components.


  • For esthetic reasons, we do not escape slashes. This also avoids a 'feature' in Apache where it 404s on any path containing '%2F'.
  • mod_rewrite unescapes %-encoded ampersands, hashes, and slashes when clean URLs are used, which are interpreted as delimiters by PHP. These characters are double escaped so PHP will still see the encoded version.
  • With clean URLs, Apache changes '//' to '/', so every second slash is double escaped.
  • This function should only be used on paths, not on query string arguments, otherwise unwanted double encoding will occur.


$text: String to encode

1 call to drupal_urlencode()
url in includes/common.inc
Generates an internal or external URL.


includes/common.inc, line 2644
Common functions that many Drupal modules will need to reference.


function drupal_urlencode($text) {
  if (variable_get('clean_url', '0')) {
    return str_replace(array(
    ), array(
    ), rawurlencode($text));
  else {
    return str_replace('%2F', '/', rawurlencode($text));


jhodgdon’s picture

In Drupal 7, the corresponding function is http://api.drupal.org/api/function/drupal_encode_path/7