PHP rawurlencode vs urlencode: Choose the Right Encoding for Each URL Component
PHP’s rawurlencode() and urlencode() both
encode strings for use inside URLs, but their conventions differ. The
right choice depends on which component you are building and what the
receiving application expects.
For a path segment, use rawurlencode(). For form-style
query values, urlencode() is appropriate. For several query
parameters, prefer http_build_query() with an explicit
encoding mode.
The difference is more than spaces
rawurlencode() follows RFC 3986 encoding: spaces become
%20, while letters, digits, and -_.~ remain
unescaped. urlencode() uses form-style encoding: spaces
become +, and the tilde becomes %7E. A literal
plus sign becomes %2B with either function. PHP
rawurlencode documentation and urlencode
documentation
$value = 'C++ guide ~ 50%';
echo rawurlencode($value);
// C%2B%2B%20guide%20~%2050%25
echo urlencode($value);
// C%2B%2B+guide+%7E+50%25
The + convention belongs to form encoding; it is not a
universal meaning of a plus sign in every URL component.
Encode values, then assemble the URL
Do not pass a complete URL through either encoder. That would encode
structural characters such as the colon and slashes in
https://, as well as query separators.
Build a controlled route from individually encoded values instead:
$slug = 'C++ notes';
$path = '/articles/' . rawurlencode($slug);
// /articles/C%2B%2B%20notes
$query = http_build_query(
['q' => 'C++ notes', 'page' => 2],
'',
'&',
PHP_QUERY_RFC3986
);
$url = '/search?' . $query;
// /search?q=C%2B%2B%20notes&page=2
PHP_QUERY_RFC3986 produces %20 for spaces.
The default PHP_QUERY_RFC1738 mode produces +.
Neither is inherently wrong for every query string: follow the receiving
API’s contract, especially when requests are signed. PHP
http_build_query documentation
Edge cases worth testing
Already encoded input. Encoding A%20B
produces A%2520B, because the percent sign is data. Keep
raw values internally and encode at the output boundary. Do not
repeatedly decode arbitrary input to “clean it up.”
Slashes in values. Encoding one segment containing
reports/2026 produces reports%2F2026. If two
route segments are intended, encode each separately and place the slash
between them yourself. For identifiers that may contain slashes, a query
parameter can avoid ambiguity about server routing.
Non-ASCII text. With UTF-8 input,
rawurlencode('Ş') produces %C5%9E. URL
encoding does not translate Turkish letters into ASCII slugs. Choose an
explicit slug policy if transliteration is required.
Decoding plus signs.
urldecode('C%2B%2B+guide') yields C++ guide;
rawurldecode() preserves the final plus instead of turning
it into a space. PHP also warns that $_GET and
$_REQUEST values are already decoded. Applying
urldecode() again can corrupt data or create security
problems. PHP
decoding guidance and already-decoded
superglobals warning
HTML escaping is a separate step
After constructing the URL, escape it for the HTML attribute where it will appear:
echo '<a href="' . htmlspecialchars(
$url,
ENT_QUOTES | ENT_SUBSTITUTE,
'UTF-8'
) . '">Search</a>';
htmlspecialchars() handles HTML-sensitive characters; it
does not replace URL-component encoding. PHP
htmlspecialchars documentation
Keep tests for spaces, literal plus signs, percent signs, slashes, tildes, quotes, and Turkish text. Verify both the generated URL and the value received by the endpoint. Encoding is transport formatting, so destination validation, authorization, and filesystem path checks still need their own logic.