URL 编码器

编码和解码 URL 字符串。

免费在线URL编码器和解码器

URLs 不能包含空格、& 符号或原始 unicode 字符 - 它们需要百分比编码。 Dokall 对字符串进行编码以便在 URLs 中安全使用,并将编码值解码回纯文本。

对于构建查询字符串、调试重定向 URLs 以及处理 Web 应用程序中的国际字符至关重要。

What is URL Encoding?

URL encoding (also called percent-encoding) converts characters that are not allowed or have special meaning in URLs into a safe format. Each unsafe character is replaced with a percent sign followed by two hexadecimal digits representing the character's ASCII or UTF-8 byte value.

For example, a space becomes %20, an ampersand (&) becomes %26, and a forward slash (/) becomes %2F. The Japanese character becomes %E6%97%A5 (three bytes in UTF-8, each percent-encoded). This ensures that URLs remain valid and unambiguous regardless of what data they carry.

Reserved and Unreserved Characters

URLs have two character categories. Unreserved characters can appear anywhere in a URL without encoding: A-Z, a-z, 0-9, hyphen (-), period (.), underscore (_), and tilde (~). These are always safe.

Reserved characters have special meaning in URL syntax: : / ? # [ ] @ ! $ & ' ( ) * + , ; =. For example, ? starts the query string, & separates query parameters, and # starts the fragment. When these characters are used as data (not as delimiters), they must be percent-encoded.

The tricky part: whether a character needs encoding depends on where it appears. A / in the path component separates directories (do not encode). A / inside a query parameter value is data (encode it). This context-dependent encoding is why there are different encoding functions.

encodeURI vs encodeURIComponent in JavaScript

JavaScript provides two encoding functions with different purposes.

encodeURI() encodes a full URI. It preserves characters that are valid in a URI: : / ? # [ ] @ ! $ & ' ( ) * + , ; =. Use it when you have a complete URL and want to encode only the characters that are never valid in any URL position (spaces, non-ASCII characters, etc.).

encodeURIComponent() encodes a URI component - a piece of a URL like a query parameter value. It encodes all reserved characters including & = ? / because these could conflict with the URL structure. Use it when building query strings: "?search=" + encodeURIComponent(userInput).

Common mistake: using encodeURI() on user input in a query string. If the user input contains &, it will not be encoded, and the browser will interpret it as a parameter separator. Always use encodeURIComponent() for query parameter values.

URL Encoding in Different Languages

In Python: urllib.parse.quote() encodes a string component (like encodeURIComponent). urllib.parse.quote_plus() encodes spaces as + instead of %20 (used in application/x-www-form-urlencoded data). urllib.parse.urlencode() encodes a dictionary into a query string.

In Java: URLEncoder.encode(str, "UTF-8") encodes for form data (spaces become +). For path encoding, use URI class constructors or a library like Apache Commons HttpClient.

In Go: url.QueryEscape() encodes for query strings (spaces become +). url.PathEscape() encodes for path segments (spaces become %20). The net/url package provides URL parsing and construction that handles encoding automatically.

In PHP: urlencode() encodes spaces as + (form encoding). rawurlencode() encodes spaces as %20 (RFC 3986). Use rawurlencode() for path components and urlencode() for form data.

Spaces in URLs: %20 vs + and When Each Applies

There are two ways to encode a space in a URL, and which one is correct depends on the context.

%20 is the standard percent-encoding for a space, defined in RFC 3986. It is correct in all URL components - path segments, query parameter names, query parameter values, and fragments.

+ is an alternative encoding for spaces, but only in the application/x-www-form-urlencoded content type - the format used by HTML form submissions. In this context (and only this context), spaces are encoded as + and + is encoded as %2B.

When building URLs for API calls, use %20. When submitting HTML forms or posting form-encoded data, + is acceptable. Most servers and frameworks handle both, but using the correct encoding avoids edge cases and ambiguity.

Frequently Asked Questions

Why do URLs need encoding?
URLs can only contain a limited set of ASCII characters. Characters like spaces, &, =, ?, and non-ASCII characters (accented letters, CJK characters) have special meanings or are simply not allowed. Encoding converts these characters into a safe percent-encoded format so the URL remains valid and the data is transmitted correctly.
What is the difference between encodeURI and encodeURIComponent?
encodeURI() encodes a full URL, preserving characters that are valid URL delimiters (: / ? # & =). encodeURIComponent() encodes a URL component (like a query parameter value), encoding all special characters including delimiters. Use encodeURIComponent() for user input in query strings. Use encodeURI() only when encoding a complete URL that already has the correct structure.
What does %20 mean in a URL?
The %20 is the percent-encoded representation of a space character. The 20 is the hexadecimal ASCII code for a space (decimal 32). Any character can be represented this way: % followed by its two-digit hex code. For example, %2F is /, %26 is &, %3D is =.
Should I encode the entire URL or just query parameters?
Encode only the parts that contain dynamic or user-supplied data - typically query parameter values and sometimes path segments. Do not encode the URL structure itself (protocol, host, path separators). For example: https://example.com/search?q= + encodeURIComponent(userQuery). Encoding the entire URL with encodeURIComponent would break the protocol and host.
How do I handle Unicode characters in URLs?
Unicode characters are first encoded to UTF-8 bytes, then each byte is percent-encoded. The character (U+65E5) becomes three UTF-8 bytes (0xE6, 0x97, 0xA5), which are encoded as %E6%97%A5. Modern browsers display the Unicode characters in the address bar but send the percent-encoded form to the server. All encoding functions in standard libraries handle this automatically.
What is the difference between + and %20 for spaces?
+ for spaces is only valid in application/x-www-form-urlencoded data (HTML form submissions). %20 is the standard percent-encoding for spaces and is correct everywhere in a URL. When building API URLs, use %20. When submitting form data, + is acceptable. Most servers handle both, but using the correct encoding avoids ambiguity.
Is URL encoding the same as HTML encoding?
No. URL encoding (percent-encoding) converts characters to %XX format for safe use in URLs. HTML encoding converts characters to entity references (&, <, ') for safe display in HTML. They solve different problems: URL encoding ensures URL validity, HTML encoding prevents XSS attacks and display issues. Using one where the other is needed will cause bugs.