๐Ÿ‹
Menu
Best Practice Beginner 1 min read 173 words

URL Encoding Best Practices for Web Developers

Proper URL encoding prevents broken links and security vulnerabilities. Learn which characters must be encoded and how to handle international URLs.

Key Takeaways

  • URLs can only contain ASCII characters.
  • Spaces become `%20` (or `+` in query strings).
  • Different URL parts have different encoding rules:
  • Domain names with non-ASCII characters use Punycode encoding.
  • A common bug is encoding already-encoded values, turning `%20` into `%2520`.

Why URL Encoding Matters

URLs can only contain ASCII characters. Special characters, spaces, and non-ASCII characters must be percent-encoded (%XX format) to be safely transmitted. Incorrect encoding causes broken links, XSS vulnerabilities, and data loss.

Characters That Must Be Encoded

Spaces become %20 (or + in query strings). Forward slashes in values must be encoded as %2F. Ampersands in values need %26 to avoid being interpreted as parameter separators.

Component-Specific Encoding

Different URL parts have different encoding rules:

  • Path: Encode spaces and special chars, preserve /.
  • Query string: Encode spaces as +, preserve & and = as separators.
  • Fragment: Encode most special characters.

International Domain Names (IDN)

Domain names with non-ASCII characters use Punycode encoding. For example, mรผnchen.de becomes xn--mnchen-3ya.de. Modern browsers display the Unicode version while transmitting the Punycode version.

Double Encoding

A common bug is encoding already-encoded values, turning %20 into %2520. This happens when a URL passes through multiple encoding layers. Always check if data is already encoded before encoding it again.

ุฃุฏูˆุงุช ุฐุงุช ุตู„ุฉ

ุตูŠุบ ุฐุงุช ุตู„ุฉ

ุฃุฏู„ุฉ ุฐุงุช ุตู„ุฉ