رمزگذاری URL
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
برخی از کاراکترها نمی توانند بخشی از یک URL باشند (مثلاً فاصله) و برخی دیگر از کاراکترها معنای خاصی در URL دارند. در فرم های HTML از کاراکتر =
برای جدا کردن یک نام از یک مقدار استفاده می شود. نحو عمومی URI از رمزگذاری URL برای مقابله با این مشکل استفاده می کند، در حالی که فرم های HTML به جای اعمال رمزگذاری درصدی برای همه این کاراکترها، برخی جایگزین های اضافی را انجام می دهند.
برای مثال، فضاهای یک رشته یا با %20
کدگذاری می شوند یا با علامت مثبت ( +
) جایگزین می شوند. اگر از یک کاراکتر لوله ( |
) به عنوان جداکننده استفاده می کنید، حتماً لوله را به صورت %7C
رمزگذاری کنید. یک کاما در یک رشته باید به صورت %2C
کدگذاری شود.
توصیه می شود از کتابخانه های ساخت URL معمولی پلتفرم خود برای رمزگذاری خودکار URL های خود استفاده کنید تا اطمینان حاصل کنید که URL ها به درستی برای پلت فرم شما فرار می کنند.
ساخت یک URL معتبر
ممکن است فکر کنید که URL "معتبر" بدیهی است، اما کاملاً اینطور نیست. برای مثال، URL وارد شده در یک نوار آدرس در مرورگر، ممکن است حاوی کاراکترهای خاصی باشد (به عنوان مثال "上海+中國"
). مرورگر باید آن کاراکترها را به صورت داخلی قبل از انتقال به رمزگذاری دیگری ترجمه کند. به همین ترتیب، هر کدی که ورودی UTF-8 را تولید یا قبول می کند، ممکن است URL های دارای کاراکترهای UTF-8 را به عنوان "معتبر" تلقی کند، اما همچنین باید آن کاراکترها را قبل از ارسال به سرور وب ترجمه کند. این فرآیند کدگذاری URL یا رمزگذاری درصد نامیده می شود.
شخصیت های خاص
ما باید کاراکترهای ویژه را ترجمه کنیم زیرا همه URL ها باید با نحو مشخص شده توسط مشخصات Uniform Resource Identifier (URI) مطابقت داشته باشند. در واقع، این بدان معنی است که URL ها باید فقط شامل یک زیرمجموعه خاص از کاراکترهای ASCII باشند: نمادهای الفبایی عددی آشنا، و برخی از کاراکترهای رزرو شده برای استفاده به عنوان کاراکترهای کنترل در URL. این جدول به طور خلاصه این شخصیت ها را نشان می دهد:
خلاصه کاراکترهای URL معتبر تنظیم کنید | شخصیت ها | استفاده از URL |
---|
الفبایی | abcdefghijklm nopqrstuvwxyz ABCDEFGHIJKLM NOPQRSTUVWXYZ 0 1 2 3 4 5 6 7 8 9 | رشته های متنی، استفاده از طرح ( http )، پورت ( 8080 )، و غیره. |
بدون رزرو | - _ . ~ | رشته های متنی |
رزرو شده است | ! * ' ( ) ; : @ & = + $ , / ? % # [ ] | کاراکترها و/یا رشته های متنی را کنترل کنید |
هنگام ساختن یک URL معتبر، باید مطمئن شوید که فقط شامل کاراکترهای نشان داده شده در جدول باشد. تطبیق یک URL برای استفاده از این مجموعه از کاراکترها به طور کلی منجر به دو مشکل می شود، یکی حذف و دیگری جایگزینی:
- کاراکترهایی که می خواهید آنها را مدیریت کنید خارج از مجموعه فوق وجود دارند. برای مثال، کاراکترهای زبانهای خارجی مانند
上海+中國
باید با استفاده از کاراکترهای بالا کدگذاری شوند. طبق قرارداد رایج، فضاها (که در URL ها مجاز نیستند) اغلب با استفاده از کاراکتر '+'
نیز نمایش داده می شوند. - کاراکترها در مجموعه فوق به عنوان کاراکترهای رزرو شده وجود دارند، اما باید به معنای واقعی کلمه استفاده شوند. به عنوان مثال
?
در URL ها برای نشان دادن ابتدای رشته پرس و جو استفاده می شود. اگر می خواهید از رشته "? and the Mysterions" استفاده کنید، باید '?'
رمزگذاری کنید. شخصیت
همه کاراکترهایی که باید با URL رمزگذاری شوند با استفاده از یک کاراکتر '%'
و یک مقدار هگز دو نویسه مطابق با نویسه UTF-8 کدگذاری می شوند. به عنوان مثال،上海+中國
در UTF-8 به صورت %E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B
کدگذاری میشود. رشته ? and the Mysterians
با URL به صورت %3F+and+the+Mysterians
یا %3F%20and%20the%20Mysterians
کدگذاری می شوند.
کاراکترهای رایج که نیاز به رمزگذاری دارند
برخی از کاراکترهای رایجی که باید کدگذاری شوند عبارتند از:
شخصیت ناامن | مقدار رمزگذاری شده |
---|
فضا | %20 |
" | %22 |
< | %3C |
> | %3E |
# | %23 |
% | %25 |
| | %7C |
تبدیل URL که از ورودی کاربر دریافت می کنید گاهی اوقات مشکل است. به عنوان مثال، کاربر ممکن است آدرسی را به عنوان "خیابان پنجم و اصلی" وارد کند. به طور کلی، شما باید URL خود را از قسمت های آن بسازید و هر ورودی کاربر را به عنوان کاراکتر تحت اللفظی در نظر بگیرید.
علاوه بر این، URLها برای همه سرویسهای وب پلتفرم Google Maps و APIهای وب استاتیک به ۱۶۳۸۴ کاراکتر محدود میشوند. برای اکثر سرویس ها، به ندرت به این محدودیت شخصیت نزدیک می شود. با این حال، توجه داشته باشید که برخی از سرویس ها دارای چندین پارامتر هستند که ممکن است منجر به URL های طولانی شود.
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eURLs can only contain a specific set of ASCII characters and some reserved characters, requiring encoding of other characters.\u003c/p\u003e\n"],["\u003cp\u003eURL encoding involves replacing unsafe characters with a '%' followed by their two-digit hexadecimal UTF-8 representation.\u003c/p\u003e\n"],["\u003cp\u003eCharacters like spaces, quotation marks, less than, greater than, hash, percent, and pipe often require URL encoding.\u003c/p\u003e\n"],["\u003cp\u003eBuilding URLs programmatically using platform libraries is recommended for automatic and proper encoding.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Maps Platform web services and static web APIs have a URL character limit of 16384.\u003c/p\u003e\n"]]],["URLs require encoding because they only accept a specific subset of ASCII characters. Special characters, like spaces and foreign language characters, must be encoded using percent encoding (e.g., `%20` for space) or plus signs in specific instances. Reserved characters, such as `=`, `?`, and `|`, also need encoding when used literally, not as control characters. It's best to utilize platform URL building libraries for automatic encoding, guaranteeing proper escaping, because it can be complex to do manually. It is also important to note that URLs are limited to 16384 characters for Google Maps Platform web services.\n"],null,["Some characters cannot be part of a URL (for example, the space) and some other\ncharacters have a special meaning in a URL. In HTML forms, the character `=` is\nused to separate a name from a value. The URI generic syntax uses URL encoding\nto deal with this problem, while HTML forms make some additional substitutions\nrather than applying percent encoding for all such characters.\n\nFor example, spaces in a string are either encoded with `%20` or replaced with\nthe plus sign (`+`). If you use a pipe character (`|`) as a separator, be sure\nto encode the pipe as `%7C`. A comma in a string should be encoded as `%2C`.\n\nIt is recommended you use your platform's normal URL building libraries to\nautomatically encode your URLs, to ensure the URLs are properly escaped for your\nplatform.\n\nBuilding a valid URL\n\nYou may think that a \"valid\" URL is self-evident, but\nthat's not quite the case. A URL entered within an address bar in a\nbrowser, for example, may contain special characters (e.g.\n`\"上海+中國\"`); the browser needs to internally translate\nthose characters into a different encoding before transmission.\nBy the same token, any code that generates or accepts UTF-8 input\nmight treat URLs with UTF-8 characters as \"valid\", but would also need\nto translate those characters before sending them out to a web server.\nThis process is called [URL-encoding](https://en.wikipedia.org/wiki/Query_string#URL_encoding) or [percent-encoding](https://en.wikipedia.org/wiki/Percent-encoding).\n\nSpecial characters\n\nWe need to translate special characters because\nall URLs need to conform to the syntax specified by the\n[Uniform\nResource Identifier (URI)](http://tools.ietf.org/html/rfc3986) specification. In effect, this means that URLs\nmust contain only a special subset of ASCII characters: the familiar\nalphanumeric symbols, and some reserved characters for use as control\ncharacters within URLs. This table summarizes these characters:\nSummary of Valid URL Characters\n\n| Set | characters | URL usage |\n|--------------|-----------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------|\n| Alphanumeric | a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 | Text strings, scheme usage (`http`), port (`8080`), etc. |\n| Unreserved | - _ . \\~ | Text strings |\n| Reserved | ! \\* ' ( ) ; : @ \\& = + $ , / ? % # \\[ \\] | Control characters and/or Text Strings |\n\nWhen building a valid URL, you must ensure that it contains only those characters shown in the\ntable. Conforming a URL to use this set of characters generally\nleads to two issues, one of omission and one of substitution:\n\n- Characters that you wish to handle exist outside of the above set. For example, characters in foreign languages such as `上海+中國` need to be encoded using the above characters. By popular convention, spaces (which are not allowed within URLs) are often represented using the plus `'+'` character as well.\n- Characters exist within the above set as reserved characters, but need to be used literally. For example, `?` is used within URLs to indicate the beginning of the query string; if you wish to use the string \"? and the Mysterions,\" you'd need to encode the `'?'` character.\n\nAll characters to be URL-encoded are encoded\nusing a `'%'` character and a two-character hex\nvalue corresponding to their UTF-8 character. For example,\n`上海+中國` in UTF-8 would be URL-encoded as\n`%E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B`. The\nstring `? and the Mysterians` would be URL-encoded as\n`%3F+and+the+Mysterians` or `%3F%20and%20the%20Mysterians`.\n\nCommon characters that need encoding\n\nSome common characters that must be encoded are:\n\n| Unsafe character | Encoded value |\n|------------------|---------------|\n| Space | `%20` |\n| \" | `%22` |\n| \\\u003c | `%3C` |\n| \\\u003e | `%3E` |\n| # | `%23` |\n| % | `%25` |\n| \\| | `%7C` |\n\nConverting a URL that you receive from user input is sometimes\ntricky. For example, a user may enter an address as \"5th\\&Main St.\"\nGenerally, you should construct your URL from its parts, treating\nany user input as literal characters.\n\nAdditionally, URLs are limited to 16384 characters for all Google Maps Platform web services\nand static web APIs. For most services, this character limit will seldom be approached. However,\nnote that certain services have several parameters that may result in long URLs."]]