URL 编码 / 解码器
这个 URL 编码解码器可将特殊字符转换为百分号编码格式(%XX),以便在 URL 中安全使用,并将编码字符串解码回可读文本。粘贴任意 URL 或文本,轻松处理保留字符、空格和 Unicode,适用于 Web 开发和 API 集成。
URL Encoder / Decoder
Encode text for safe use in URLs or decode URL-encoded strings
常见问题
什么是URL编码?
URL编码也称为百分比编码,是将字符转换为可在URL中安全传输格式的过程。URL中不允许的字符(如空格、&、=和非ASCII字符)会被替换为百分号(%)后跟其两位十六进制值。例如,空格变为%20。
为什么URL需要编码?
URL只能包含有限的ASCII字符集。空格、&符号、等号和非英语字符会破坏URL解析或被错误解释。编码确保数据在查询字符串、表单提交和API请求中正确传输,而不会与URL语法分隔符冲突。
哪些字符需要URL编码?
必须编码的字符包括:空格、非ASCII字符(如带重音的字母和CJK字符),以及作为数据而非分隔符使用时的保留字符。保留字符包括:: / ? # [ ] @ ! $ & ' ( ) * + , ; =。不需要编码的非保留字符(A-Z、a-z、0-9、-、_、.、~)永远不需要编码。
什么是百分比编码?
百分比编码是RFC 3986定义的URL编码的正式名称。每个字符被转换为其UTF-8字节序列,每个字节表示为%XX,其中XX是十六进制值。例如,欧元符号编码为%E2%82%AC(三个UTF-8字节)。"百分比编码"这个术语来源于每个编码字节中使用的%前缀。
encodeURI和encodeURIComponent有什么区别?
在JavaScript中,encodeURI()对完整URI进行编码,但保留在URL中具有特殊含义的字符(如:、/、?、#、&)。encodeURIComponent()对除不需要编码字符外的所有内容进行编码,适合编码单个查询参数值。对完整URL使用encodeURI,对参数值使用encodeURIComponent。
如何解码URL?
URL解码通过将%XX序列转换回原始字符来逆转百分比编码。在JavaScript中,对完整URL使用decodeURI(),对单个组件使用decodeURIComponent()。在Python中,使用urllib.parse.unquote()。大多数编程语言都提供内置的URL解码函数,可以自动处理UTF-8多字节序列。
URL中空格用加号还是%20表示?
+和%20都表示空格,但在不同场景下使用。+号代替空格来自HTML表单提交使用的application/x-www-form-urlencoded格式。%20是RFC 3986中定义的标准百分比编码。在URL路径中,始终使用%20。在查询字符串中,两者都可接受,但%20与各系统的兼容性更好。
如何在Python、Java或PHP中进行URL编码?
在Python中,使用urllib.parse.quote()编码,urllib.parse.unquote()解码。在Java中,使用URLEncoder.encode(str, "UTF-8")和URLDecoder.decode(str, "UTF-8")。在PHP中,使用urlencode()进行表单风格编码(空格用+号),或rawurlencode()进行RFC 3986编码(空格用%20)。解码分别使用urldecode()或rawurldecode()。
什么是双重编码,如何避免?
双重编码是指对已编码的字符串再次编码,将%20变为%2520(%本身被编码为%25)。这会破坏URL,因为服务器接收到的%20是字面文本而不是空格。为避免双重编码,在重新编码前始终完全解码字符串,永远不要对已包含有效百分比编码字符的完整URL进行编码。
URL编码和HTML编码有什么区别?
URL编码(百分比编码)将字符转换为%XX格式用于URL中。HTML编码将字符转换为命名实体(&、<、>)或数字实体(&、<)以便在HTML文档中安全显示。它们的用途不同:URL编码防止URL解析错误,而HTML编码防止XSS攻击并确保特殊字符在网页中正确显示。
About URL Encoder / Decoder
The URL Encoder / Decoder is an essential tool for web developers that converts text into URL-safe format using percent-encoding, or decodes percent-encoded strings back to readable text. URL encoding ensures that special characters are properly transmitted in URLs without causing parsing errors or ambiguity.
What is URL Encoding?
URL encoding, also known as percent-encoding, is a mechanism for encoding characters that have special meaning in URLs or that are not allowed in URLs. Each encoded character is replaced with a percent sign (%) followed by two hexadecimal digits representing the character's byte value. For example, a space character becomes %20, and an ampersand (&) becomes %26.
How to Use the URL Encoder / Decoder
- Select the "Encode" tab to convert text to URL-safe format, or the "Decode" tab to convert back to plain text
- Type or paste your text into the input area
- The URL Encoder / Decoder converts your input instantly in real time
- View which specific characters were encoded in the breakdown
- Click the copy button to copy the result to your clipboard
Characters That Need URL Encoding
The following characters have special meanings in URLs and must be encoded when used as data values:
| Character | Encoded | Purpose in URL |
|---|---|---|
| space | %20 | Separator |
| ! | %21 | Sub-delimiter |
| # | %23 | Fragment identifier |
| $ | %24 | Sub-delimiter |
| & | %26 | Parameter separator |
| ' | %27 | Sub-delimiter |
| ( | %28 | Sub-delimiter |
| ) | %29 | Sub-delimiter |
| + | %2B | Space (in forms) |
| / | %2F | Path separator |
| : | %3A | Scheme separator |
| = | %3D | Key-value separator |
| ? | %3F | Query string start |
| @ | %40 | User info separator |
Common Use Cases for URL Encoding
- Encoding query string parameters that contain special characters
- Preparing user-submitted data for inclusion in URLs
- Encoding file paths with spaces or special characters
- Building API request URLs with dynamic parameters
- Encoding redirect URLs passed as parameters
- Handling internationalized domain names and paths with non-ASCII characters
encodeURIComponent vs encodeURI
This URL Encoder / Decoder uses encodeURIComponent(), which encodes all characters that are not unreserved (letters, digits, hyphens, periods, underscores, and tildes). This is the appropriate function for encoding individual URL components like query parameter values. The alternative, encodeURI(), is designed for encoding entire URIs and preserves characters like :, /, ?, and # that have structural meaning in URLs.
URL Encoding in Programming Languages
| Language | Encode | Decode |
|---|---|---|
| JavaScript | encodeURIComponent(str) | decodeURIComponent(str) |
| Python | urllib.parse.quote(str) | urllib.parse.unquote(str) |
| Java | URLEncoder.encode(str, "UTF-8") | URLDecoder.decode(str, "UTF-8") |
| PHP | rawurlencode($str) | rawurldecode($str) |
| C# | Uri.EscapeDataString(str) | Uri.UnescapeDataString(str) |
| Go | url.QueryEscape(str) | url.QueryUnescape(str) |
| Ruby | CGI.escape(str) | CGI.unescape(str) |
Tips for URL Encoding
- Always encode user input before including it in URLs to prevent injection attacks
- Use
encodeURIComponent()for query parameter values, not for entire URLs - Remember that spaces can be encoded as
%20or+depending on the context - Double-encoding (encoding an already-encoded string) can cause issues — always decode before re-encoding
- Unicode characters are first converted to UTF-8 bytes, then each byte is percent-encoded