URL 編碼/解碼工具
使用此在線編碼和解碼工具將特殊字符轉換為 URL 安全格式或從中轉換。
常見問題
什麼是 URL 編碼?
URL 編碼是一種將字符轉換為可以在網際網路上傳輸的格式的方法。URL 只能使用 ASCII 字符集通過網際網路發送。由於 URL 經常包含 ASCII 集之外的字符,因此需要轉換 URL。URL 編碼將不安全的 ASCII 字符替換為「%」後跟兩個十六進制數字。
URL 編碼如何運作?
URL 編碼通過將不安全字符替換為「%」後跟表示該字符 UTF-8 編碼的兩個十六進制數字來運作。例如,空格字符編碼為 %20。URL 不能包含空格,因此它們被替換為加號 (+) 或 %20。其他特殊字符則被替換為對應的 %xx 代碼。
常見需要 URL 編碼的字符
以下是在 URL 中使用時需要 URL 編碼的常見字符:
保留字元
| 字符 | URL編碼 | 說明 |
|---|---|---|
| # | %23 | 井號(用於URL片段) |
| $ | %24 | 美元符號 |
| & | %26 | 連字號(URL參數分隔符) |
| + | %2B | 加號 |
| , | %2C | 逗號 |
| / | %2F | 斜線(URL路徑分隔符) |
| : | %3A | 冒號 |
| ; | %3B | 分號 |
| = | %3D | 等號(URL參數賦值符號) |
| ? | %3F | 問號(URL查詢字串開始符號) |
| @ | %40 | at符號 |
| [ | %5B | 左方括號 |
| ] | %5D | 右方括號 |
其他常用字元
| 字符 | URL編碼 | 說明 |
|---|---|---|
| Space | %20 | URL中最常見的需要編碼的字符 |
| ! | %21 | 驚嘆號 |
| " | %22 | 雙引號 |
| % | %25 | 百分比符號(URL編碼的跳脫字符) |
| ' | %27 | 單引號 |
| ( | %28 | 左括號 |
| ) | %29 | 右括號 |
| * | %2A | 星號 |
| \ | %5C | 反斜線 |
| ^ | %5E | 插入符號 |
| { | %7B | 左花括號 |
| | | %7C | 豎線 |
| } | %7D | 右花括號 |
| ~ | %7E | 波浪符 |
不同程式語言中的 URL 編碼實現
以下是各種程式語言中 URL 編碼和解碼的示例:
Frontend / Scripting
JavaScript
// URL encoding
const text = "Hello World! Special chars: &?=/";
const encoded = encodeURIComponent(text);
console.log("Encoded:", encoded);
// URL decoding
const decoded = decodeURIComponent(encoded);
console.log("Decoded:", decoded);TypeScript
// URL encoding
const text: string = "Hello World! Special chars: &?=/";
const encoded: string = encodeURIComponent(text);
console.log("Encoded:", encoded);
// URL decoding
const decoded: string = decodeURIComponent(encoded);
console.log("Decoded:", decoded);Python
import urllib.parse
# URL encoding
text = "Hello World! Special chars: &?=/"
encoded = urllib.parse.quote(text)
print(f"Encoded: {encoded}")
# URL decoding
decoded = urllib.parse.unquote(encoded)
print(f"Decoded: {decoded}")Backend / Systems
Go
package main
import (
"fmt"
"net/url"
)
func main() {
// Encode a URL
text := "Hello World! Special chars: &?=/";
encoded := url.QueryEscape(text)
fmt.Println("Encoded:", encoded)
// Decode a URL
decoded, err := url.QueryUnescape(encoded)
if err == nil {
fmt.Println("Decoded:", decoded)
}
}PHP
<?php
// URL encoding
$text = "Hello World! Special chars: &?=/";
$encoded = urlencode($text);
echo "Encoded: " . $encoded . "\n";
// URL decoding
$decoded = urldecode($encoded);
echo "Decoded: " . $decoded . "\n";
?>C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Function to URL-encode a string
char *url_encode(char *str) {
char *encoded = malloc(strlen(str) * 3 + 1);
char *pstr = str;
char *pbuf = encoded;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
*pbuf++ = *pstr;
} else if (*pstr == ' ') {
*pbuf++ = '+';
} else {
sprintf(pbuf, "%%%.2X", *pstr);
pbuf += 3;
}
pstr++;
}
*pbuf = '\0';
return encoded;
}
int main() {
char *text = "Hello World! Special chars: &?=/";
char *encoded = url_encode(text);
printf("Original: %s\n", text);
printf("Encoded: %s\n", encoded);
free(encoded);
return 0;
}encodeURI 和 encodeURIComponent 有什麼區別?
encodeURI() 設計用於編碼完整的 URI,因此它不會編碼在 URL 中具有特殊意義的字符,例如 /、?、:、@、&、=、+、$ 和 #。 相比之下,encodeURIComponent() 會編碼所有特殊字符,使其適合用於編碼 URL 的部分,例如查詢字串參數。 在編碼查詢參數時,應始終使用 encodeURIComponent() 以確保所有特殊字符都被正確編碼。
URL 編碼排錯清單
二次編碼
如果看到 %2520,表示 %20 裡的百分號又被編碼成了 %25。重新編碼前先解碼一次,確認真實值。
空格是 + 還是 %20
HTML 表單編碼常把空格轉成 +,而 URL 元件編碼通常使用 %20。做簽名、快取 key 或介面比對前,要確認後端期望哪一種。
路徑與查詢值
斜線在路徑中有結構意義,但在查詢參數值裡通常應該編碼。應編碼單個參數值,而不是已經組好的完整 URL。
Unicode 參數
中文、emoji 和其他非 ASCII 文字會先轉成 UTF-8 位元組,再做百分號編碼。亂碼通常表示原始文字用了其他字元編碼。
URL 編碼實用場景
更穩妥的做法是在組合查詢字串之前先編碼參數值。下面這些例子說明應該編碼哪一段,以及為什麼要編碼。
帶空格的搜尋詞
- 原始值
q=hello world- 編碼後
q=hello%20world
查詢值中的空格需要編碼。表單提交有時使用 +,但通用 URL 元件中 %20 更穩妥。
把回呼 URL 作為參數
- 原始值
redirect=https://example.com/a?x=1&y=2- 編碼後
redirect=https%3A%2F%2Fexample.com%2Fa%3Fx%3D1%26y%3D2
巢狀 URL 的 ? 和 & 必須作為參數值編碼,否則會破壞外層查詢字串。
需要保留的加號
- 原始值
phone=+1 555 0100- 編碼後
phone=%2B1%20555%200100
未編碼的 + 可能被表單解碼器當成空格。加號有實際含義時應寫成 %2B。
Unicode 關鍵詞
- 原始值
tag=中文✅- 編碼後
tag=%E4%B8%AD%E6%96%87%E2%9C%85
非 ASCII 文字會先轉成 UTF-8 位元組,再進行百分號編碼。