شرح ترميز URL: دليل كامل
عناوين URL هي العمود الفقري للويب، لكنها لا تستطيع التعامل مع جميع الأحرف مباشرة. ترميز URL (المعروف أيضًا باسم ترميز النسبة المئوية) يحول الأحرف الخاصة إلى تنسيق يمكن نقله بأمان عبر الإنترنت. سواء كنت تبني واجهات برمجة تطبيقات أو تعالج معلمات استعلام أو تتعامل مع مدخلات المستخدم، فإن فهم ترميز URL ضروري لتطوير الويب.
ما هو ترميز URL؟
ترميز URL، رسميًا ترميز النسبة المئوية، هو آلية لترميز المعلومات في معرف الموارد الموحد (URI). يستبدل الأحرف غير الآمنة بعلامة النسبة المئوية (%) متبوعة برقمين ست عشريين يمثلان قيمة ASCII للحرف. على سبيل المثال، يتم ترميز المسافة كـ %20 وعلامة التعجب كـ %21.
عناوين URL مقيدة بمجموعة أحرف ASCII. أي حرف خارج هذه المجموعة، أو أي حرف له معنى خاص في عنوان URL (مثل & و = و #)، يجب ترميزه. بدون الترميز، يمكن أن تفسر الأحرف الخاصة بشكل خاطئ كمحددات URL، مما يكسر وظيفة الرابط أو ينشئ ثغرات أمنية.
ترميز URL في JavaScript
يجب ترميز الأحرف التالية دائمًا في معلمات استعلام URL:
تقدم كل لغة برمجة وظائف مدمجة لترميز URL. إليك الاستخدام الصحيح في اللغات الرئيسية:
لماذا ترميز URL ضروري
يجب ترميز معلمات استعلام URL دائمًا قبل إضافتها إلى عنوان URL. يجب ترميز أجزاء المسار فقط إذا كانت تحتوي على أحرف غير ASCII أو أحرف URL محجوزة. يجب ترميز معلمات POST عندما تكون بيانات النموذج من نوع application/x-www-form-urlencoded. لا تقم بترميز عناوين URL كاملة دفعة واحدة — قم بترميز المكونات الفردية.
- المسافة ( ) -> %20
- علامة التعجب (!) -> %21
- علامة الاقتباس (") -> %22
ترميز URL في Python
لا تقم بترميز عناوين URL كاملة — قم بترميز المكونات الفردية فقط. لا تقم بترميز عناوين URL التي تم ترميزها بالفعل. لا تقم بترميز الأحرف المحجوزة في أجزاء المسار التي لها معنى نحوي. لا تقم بترميز عناوين URL كاملة التسلسل.
الترميز المزدوج هو خطأ ترميز شائع حيث يتم ترميز سلسلة تم ترميزها بالفعل مرة أخرى. على سبيل المثال، يتم ترميز المسافة إلى %20، ثم يتم ترميز %20 إلى %2520 (يتم ترميز % إلى %25). يحدث هذا غالبًا عندما تطبق أطر العمل طبقات ترميز متعددة.
مرجع أحرف ترميز URL
استخدم أدوات ترميز URL المجانية الخاصة بنا لترميز وفك ترميز عناوين URL.
| الوظيفة (JavaScript) | الغرض | الاستخدام الصحيح | الوظيفة (Python) |
|---|---|---|---|
| encodeURIComponent() | ترميز قيمة معلمة استعلام | ؟q=encodeURIComponent('hello world') | encodeURI() |
| ترميز عنوان URL كامل | encodeURI('https://example.com/hello world') | decodeURIComponent() | فك ترميز قيمة معلمة استعلام |
| decodeURIComponent('hello%20world') | decodeURI() | فك ترميز عنوان URL كامل | decodeURI('https://example.com/hello%20world') |
| urllib.parse.quote() | ترميز قيمة معلمة استعلام | quote('hello world') | urllib.parse.quote_plus() |
| ترميز مع ترميز + للمسافات | quote_plus('hello world') | urllib.parse.unquote() | فك ترميز قيمة معلمة استعلام |
| unquote('hello%20world') | urllib.parse.urlencode() | ترميز قاموس إلى سلسلة استعلام | urlencode({'q': 'hello world'}) |
| urlencode() | ترميز قيمة معلمة استعلام | urlencode('hello world') | rawurlencode() |
| ترميز بمعيار RFC 3986 (المسافات كـ %20) | rawurlencode('hello world') | urldecode() | فك ترميز قيمة معلمة استعلام |
| urldecode('hello%20world') | http_build_query() | ترميز مصفوفة إلى سلسلة استعلام | http_build_query(['q' => 'hello world']) |
| , | %2C | 44 (0x2C) | Reserved (sub-delim) |
| / | %2F | 47 (0x2F) | Reserved (path separator) |
| : | %3A | 58 (0x3A) | Reserved (scheme delimiter) |
| ; | %3B | 59 (0x3B) | Reserved (param separator) |
| = | %3D | 61 (0x3D) | Reserved (value delimiter) |
| ? | %3F | 63 (0x3F) | Reserved (query delimiter) |
| @ | %40 | 64 (0x40) | Reserved (authority delimiter) |
| [ | %5B | 91 (0x5B) | Reserved (IPv6 literal) |
| ] | %5D | 93 (0x5D) | Reserved (IPv6 literal) |
ترميز URL في لغات البرمجة
ترميز URL، أو ترميز النسبة المئوية، هو آلية تحول الأحرف الخاصة إلى تنسيق يمكن نقله بأمان في عناوين URL. يستبدل الأحرف غير الآمنة بـ % متبوعًا برقمين ست عشريين. على سبيل المثال، يتم ترميز المسافة كـ %20. يضمن الترميز نقل عنوان URL بشكل صحيح عبر الإنترنت.
ترميز URL في PHP
encodeURI() مصمم لعناوين URL الكاملة. يحافظ على الأحرف المحجوزة التي لها معنى نحوي في عنوان URL (مثل / و ? و & و #). encodeURIComponent() مصمم لأجزاء عنوان URL الفردية. يرمّز جميع الأحرف المحجوزة. استخدم encodeURIComponent() لمعلمات الاستعلام وقيم النماذج.
متى تستخدم الترميز
نعم، ترميز URL قابل للعكس تمامًا. وظيفة فك الترميز (مثل decodeURIComponent() في JavaScript) تحول تسلسل الترميز بالنسبة المئوية مرة أخرى إلى الأحرف الأصلية. فك الترميز يطابق الترميز تمامًا.
متى يكون الترميز غير ضروري
| الغرض | الاستخدام الصحيح | الوظيفة (PHP) |
|---|---|---|
| Context | URLs and URIs | HTML documents |
| Format | %XX (percent + hex) | &name; or &#NNN; |
| Example for < | %3C | < |
| Example for & | %26 | & |
| Example for " | %22 | " |
| Purpose | Safe URL transmission | Prevent HTML injection |
| Specification | RFC 3986 | HTML Living Standard |
يمكن ترميز المسافات كـ %20 أو +. في معلمات استعلام URL (الجزء بعد ?)، يتم ترميز المسافات تقليديًا كـ +. في أجزاء URL الأخرى، تستخدم المسافات %20. دالة encodeURIComponent() في JavaScript تستخدم %20، بينما العديد من اللغات من جانب الخادم تستخدم + لمعلمات الاستعلام.
ترميز URL مقابل ترميز HTML
ترميز النسبة المئوية هو اسم آخر لترميز URL. يحول الأحرف الخاصة إلى تسلسلات % متبوعة بنقطة ترميز الحرف الست عشرية. يمثل %20 مسافة، %23 يمثل #، %26 يمثل &، وهكذا.
ما هو ترميز URL؟
JavaScript provides three encoding functions, each with different behavior:
// encodeURI - encodes a complete URL
// Preserves: :, /, ?, #, &, =, +, @, ;, ,, !, ~, *, ', (, )
const url = encodeURI("https://example.com/search?q=hello world");
// Result: "https://example.com/search?q=hello%20world"
// encodeURIComponent - encodes a URL component
// Encodes ALL special characters including URL delimiters
const param = encodeURIComponent("price=100&discount=20");
// Result: "price%3D100%26discount%3D20"
// Never use escape() - it is deprecated
// It does not handle Unicode correctlyencodeURIencodeURIComponentencodeURIencodeURIComponent when you are encoding a single
parameter value.
ما الفرق بين encodeURI و encodeURIComponent؟
from urllib.parse import quote, quote_plus, urlencode
# quote - standard URL encoding (spaces as %20)
encoded = quote("hello world&more")
# Result: "hello%20world%26more"
# quote_plus - spaces become + instead of %20
encoded_plus = quote_plus("hello world")
# Result: "hello+world"
# urlencode - encodes a dictionary as a query string
params = {"q": "hello world", "lang": "en"}
query = urlencode(params)
# Result: "q=hello+world&lang=en"هل يمكن عكس ترميز URL؟
// urlencode - spaces become +
$encoded = urlencode("hello world&more");
// Result: "hello+world%26more"
// rawurlencode - RFC 3986 compliant (spaces as %20)
$raw = rawurlencode("hello world&more");
// Result: "hello%20world%26more"الأخطاء الشائعة
The space character has two common encodings, and understanding when to use each is important:
- علامة التجزئة (#) -> %23
- علامة الدولار ($) -> %24
+application/x-www-form-urlencoded+ is a literal plus sign, not a space. Most
server-side frameworks handle this correctly for query strings, but it
can cause subtle bugs when encoding paths or working with custom URL
schemes.
أفضل الممارسات
URL encoding has significant security implications that every web developer must understand.
كيفية التعامل مع المسافات في عناوين URL؟
%2527%27' on the second pass. If a security filter only checks
the first decode, it would miss the malicious character. This can
bypass input validation, cross-site scripting (XSS) filters, and SQL
injection protections.
<script>%253Cscript%253E<script>, executing the attack.
ما هو ترميز النسبة المئوية؟
A common misconception is that URL encoding hides or protects data. It does not. URL encoding is trivially reversible; anyone can decode a percent-encoded string. Never use URL encoding as a substitute for encryption, authentication, or access control. Sensitive data like passwords, API keys, and personal information should never appear in URLs, encoded or not, because URLs are logged by browsers, servers, proxies, and can be visible in referrer headers.
Open Redirect Vulnerabilities
/redirect?url=https%3A%2F%2Fevil.com. If the application
does not validate the target URL after decoding, attackers can use
this to redirect users to phishing sites. Always validate decoded URL
values against an allowlist of permitted domains.
Path Traversal
../../etc/passwd%2e%2e%2f%2e%2e%2f%65%74%63%2f%70%61%73%73%77%64. Web
servers must decode and normalize paths before checking for traversal
attacks.
الأسئلة الشائعة
Encoding Query Parameters
When building URLs with dynamic query parameters, always encode the values:
const baseUrl = "https://api.example.com/search";
const query = "user input with special chars: & ? = #";
const url = `${baseUrl}?q=${encodeURIComponent(query)}`;
// Result: "https://api.example.com/search?q=user%20input%20with%20special%20chars%3A%20%26%20%3F%20%3D%20%23"Encoding URL Paths
Path segments that contain special characters need encoding, but you must not encode the path separators:
const category = "electronics & gadgets";
const product = "USB-C cable (6ft)";
const url = `/products/${encodeURIComponent(category)}/${encodeURIComponent(product)}`;
// Result: "/products/electronics%20%26%20gadgets/USB-C%20cable%20(6ft)"Decoding URL Components
const encoded = "hello%20world%3F%26%3D%23";
const decoded = decodeURIComponent(encoded);
// Result: "hello world?&=#"Common Pitfalls
- علامة النسبة المئوية (%) -> %25
- علامة العطف (&) -> %26
- علامة الاقتباس المفردة (') -> %27
- علامة الجمع (+) -> %2B (أو تستخدم كمساحة في معلمات الاستعلام)
- الفاصلة (,) -> %2C
Need to encode or decode URLs quickly? Try our free online URL encoder and decoder tools.
أداة ترميز URLDecode URLFrequently Asked Questions
What is URL encoding?
URL encoding, also called percent-encoding, is a mechanism that converts characters into a format that can be safely transmitted in a URL. It replaces unsafe or reserved characters with a percent sign followed by two hexadecimal digits representing the character's byte value. For example, a space becomes %20 and a question mark becomes %3F.
Why do some URLs contain %20?
%20 is the URL-encoded representation of a space character. Spaces are not allowed in URLs because they can be ambiguous and break URL parsing. The percent sign followed by 20 represents the hexadecimal value 0x20, which is the ASCII code for a space. Some systems also use + for spaces in query strings, but %20 is the standard encoding.
What is the difference between URL encoding and HTML encoding?
URL encoding converts characters to percent-encoded format (%XX) for safe transmission in URLs. HTML encoding converts characters to entity references (&, <, etc.) for safe display in HTML documents. They serve different purposes: URL encoding is for URLs, HTML encoding is for HTML content. A value like < needs both: %3C in a URL and < in HTML.
When should I use encodeURIComponent vs encodeURI?
Use encodeURI when encoding a complete URL, as it preserves URL structure characters like :, /, ?, &, and =. Use encodeURIComponent when encoding a single URL component value (like a query parameter), as it encodes ALL special characters including those that have structural meaning in URLs. Always use encodeURIComponent for query parameter values.
Can URL encoding be used for security?
URL encoding alone is not a security measure. It is a transport mechanism, not encryption or authentication. In fact, double encoding attacks can bypass security filters by encoding malicious input multiple times. Always validate and sanitize input on the server side, use parameterized queries for databases, and apply proper output encoding for the target context (HTML, URL, JavaScript).