URL Encoding Guide: อักขระที่ปลอดภัยและกรณีพิเศษ
เรียนรู้ URL encoding มาสเตอร์อักขระที่ต้องเข้ารหัส ตัวอย่างจริง และวิธีใช้ URL encoders อย่างปลอดภัย

Table of Contents
URL Encoding Guide: อักขระที่ปลอดภัยและกรณีพิเศษ
URL ควรมีอักขระเฉพาะเท่านั้น เมื่อต้องรวมอักขระพิเศษ URL encoding คือวิธีแก้ปัญหา คู่มือนี้สอนให้คุณทั้งหมดที่คุณต้องรู้
URL Encoding คืออะไร?
นิยาม
URL encoding (เรียกว่า percent-encoding) แปลงอักขระพิเศษให้เป็นรูปแบบที่ปลอดภัยที่ URL สามารถส่งได้
ต้นฉบับ: Hello World! Encoded: Hello%20World%21 ต้นฉบับ: [email protected] Encoded: user%40example.com
ทำไมจึงจำเป็น
URL มีข้อจำกัด:
- อักขระเว้นวรรคอาจทำให้การแยกวิเคราะห์ขาด
- อักขระบางตัวมีความหมายพิเศษ (?=&)
- ระบบต่างๆ ตีความอักขระต่างกัน
- URL encoding ช่วยรับประกันความเข้ากันได้
อักขระที่ปลอดภัยและไม่ปลอดภัย
อักขระที่ปลอดภัย
อักขระเหล่านี้ปลอดภัยเสมอใน URL:
ตัวอักษร: A-Z, a-z ตัวเลข: 0-9 ไม่มีสงวน: - . _ ~ (ไม่ต้องเข้ารหัส)
ตัวอย่าง:
✅ https://example.com/my-article ✅ https://example.com/blog_2025 ✅ https://example.com/item.php
อักขระที่ไม่ปลอดภัย
ต้องเข้ารหัส:
ตัวอักษร เข้ารหัสเป็น กรณีการใช้ Space %20 ระหว่างคำ ! %21 จุดสิ้นสุดข้อความ " %22 เครื่องหมายอัญประลัษณ์ # %23 การอ้างอิงส่วนหัว & %26 ตัวคั่นพารามิเตอร์ @ %40 สัญลักษณ์ที่ = %3D ค่าพารามิเตอร์ ? %3F จุดสนใจ / %2F เครื่องหมายทับ : %3A โคลอน
ตัวอย่าง URL Encoding ทั่วไป
ตัวอย่างที่ 1: Query Search
ต้นฉบับ: "best online tools" Encoded: "best%20online%20tools"
URL:
https://example.com/search?q=best%20online%20tools
ตัวอย่างที่ 2: Email ใน URL
ต้นฉบับ: [email protected] Encoded: user%40example.com
URL:
https://example.com/user/user%40example.com
ตัวอย่างที่ 3: ชื่อไฟล์
ต้นฉบับ: "my document.pdf" Encoded: "my%20document.pdf"
URL:
https://example.com/downloads/my%20document.pdf
ตัวอย่างที่ 4: พารามิเตอร์พิเศษ
ต้นฉบับ: price=$100&discount=50% Encoded: price=%24100&discount=50%25
URL:
https://shop.com?price=%24100&discount=50%25
URL Encoding vs อื่นๆ
บริบทต่างกัน กฎต่างกัน
// URL Encoding (สำหรับ URL)
"hello world" → "hello%20world"
// HTML Entity Encoding (สำหรับ HTML)
"hello world" → "hello world" (ไม่มีการเปลี่ยน)
"<script>" → "<script>"
// JavaScript Encoding (สำหรับ JavaScript)
"alert('hi')" → "alert(\'hi\')" (หนีเครื่องหมายคำพูด)
เมื่อใดที่จะใช้แต่ละอย่าง
| บริบท | Encoding | ตัวอย่าง |
|---|---|---|
| URL parameters | URL encode | ?name=hello%20world |
| HTML attributes | URL or HTML encode | <a href="...%20..."> |
| JavaScript strings | JavaScript escape | var x = "hello\nworld" |
| JSON data | JSON escape | {"name":"hello\"world"} |
เครื่องมือ URL Encoding
ใช้ Online Encoder
ขั้นตอนที่ 1: ป้อนข้อความ
[email protected] 2025
ขั้นตอนที่ 2: เลือกการดำเนินการ
- Encode (เพิ่มโค้ด %)
- Decode (ลบโค้ด %)
ขั้นตอนที่ 3: ได้ผลลัพธ์
user%40example.com%202025
เครื่องมือ URL Encoder/Decoder
ตัวอย่างการเขียนโปรแกรม
JavaScript:
// เข้ารหัส
encodeURIComponent('hello world!');
// ผลลัพธ์: "hello%20world%21"
// ถอดรหัส
decodeURIComponent('hello%20world%21');
// ผลลัพธ์: "hello world!"
Python:
from urllib.parse import quote, unquote
# เข้ารหัส
quote("hello world!")
# ผลลัพธ์: "hello%20world%21"
# ถอดรหัส
unquote("hello%20world%21")
# ผลลัพธ์: "hello world!"
กรณีการใช้จริง
กรณีที่ 1: สร้าง Search URL
const searchTerm = 'best tools 2025';
const encodedTerm = encodeURIComponent(searchTerm);
const url = `https://google.com/search?q=${encodedTerm}`;
// ผลลัพธ์: https://google.com/search?q=best%20tools%202025
กรณีที่ 2: สร้างลิงก์ Download
<!-- ผิด: เว้นวรรคทำให้ลิงก์ขาด --> <a href="/downloads/my document.pdf">Download</a> <!-- ถูก: ชื่อไฟล์ที่เข้ารหัสใช้ได้ --> <a href="/downloads/my%20document.pdf">Download</a>
กรณีที่ 3: พารามิเตอร์ API
const params = {
email: '[email protected]',
message: 'Hello, World!',
amount: '$100',
};
const urlParams = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
urlParams.append(key, value);
});
const apiUrl = `https://api.example.com?${urlParams}`;
// เบราว์เซอร์เข้ารหัส URL ค่าโดยอัตโนมัติ
ความผิดพลาดทั่วไป
ความผิดพลาด 1: Double Encoding
// ผิด - เข้ารหัสสองครั้ง
const encoded = encodeURIComponent(encodeURIComponent('hello world'));
// ผลลัพธ์: "hello%2520world" (% เองถูกเข้ารหัส!)
// ถูก - เข้ารหัสเพียงครั้งเดียว
const encoded = encodeURIComponent('hello world');
// ผลลัพธ์: "hello%20world"
ความผิดพลาด 2: ไม่เข้ารหัสพารามิเตอร์ Query
// ผิด - อักขระพิเศษทำให้ query ขาด const url = `https://[email protected]&name=John Doe`; // ปัญหา: & และเว้นวรรค // ถูก - เข้ารหัสค่าพารามิเตอร์ const email = encodeURIComponent('[email protected]'); const name = encodeURIComponent('John Doe'); const url = `https://api.example.com?email=${email}&name=${name}`;
ความผิดพลาด 3: เข้ารหัส URL ทั้งหมด
// ผิด - เข้ารหัส slashes และ colons!
encodeURIComponent('https://example.com/path');
// ผลลัพธ์: "https%3A%2F%2Fexample.com%2Fpath"
// ถูก - เข้ารหัสเฉพาะส่วนต่างๆ
const path = 'search results';
const encoded = encodeURIComponent(path);
const url = `https://example.com/search/${encoded}`;
โครงสร้าง URL และการเข้ารหัส
โครงสร้าง URL
https://user:[email protected]:8080/path/to/page?name=value#section \_____/ \___/ \___/\__________/\__/ \_____________/ \___________/ \_____/ protocol user pass domain port path query fragment
สิ่งที่ต้องเข้ารหัส
| ส่วน | Encoding | ตัวอย่าง |
|---|---|---|
| Protocol | ไม่ต้อง | https:// (ปล่อยไว้เดิม) |
| Domain | ไม่ต้อง | example.com (ปล่อยไว้เดิม) |
| Path | เข้ารหัส | /search/my%20query |
| Query values | เข้ารหัส | ?q=hello%20world |
| Fragment | บางครั้ง | #my%20section |
คำถามที่พบบ่อย
Q: เมื่อไหร่ที่ควรเข้ารหัส URL? A: เมื่ออักขระพิเศษปรากฏใน query string หรือส่วนต่าง ๆ ของ URL
Q: URL encoding เหมือนกับการเข้ารหัสหรือไม่? A: ไม่ URL encoding กลับด้านได้และมองเห็นได้ การเข้ารหัสปกป้องด้วยคีย์
Q: Double encoding ทำอะไรได้บ้าง? A: ทำลาย URL % ตัวเองถูกเข้ารหัสใหม่
Q: ต้องเข้ารหัสใน HTTPS หรือไม่? A: ใช่ HTTPS เข้ารหัสข้อมูล แต่ URL encoding ยังคงจำเป็นสำหรับ URL ที่ถูกต้อง
Q: เบราว์เซอร์ถอดรหัส URL โดยอัตโนมัติหรือไม่? A: ใช่ เบราว์เซอร์ถอดรหัส %20 เป็นเว้นวรรค %3F เป็น ? แต่คุณควรเข้ารหัสเพื่อความปลอดภัย
ทรัพยากร
เครื่องมือ URL Encoder/Decoder - เข้ารหัส/ถอดรหัสอย่างรวดเร็ว
Guides ที่เกี่ยวข้อง:
อัปเดต: พฤศจิกายน 2025 | เวลาอ่าน: 9 นาที