Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Một số ký tự không thể nằm trong URL (ví dụ: dấu cách) và một số ký tự khác có ý nghĩa đặc biệt trong URL. Trong biểu mẫu HTML, ký tự = được dùng để phân tách tên khỏi giá trị. Cú pháp chung của URI sử dụng phương thức mã hoá URL để giải quyết vấn đề này, trong khi các biểu mẫu HTML thực hiện một số thay thế bổ sung thay vì áp dụng phương thức mã hoá phần trăm cho tất cả các ký tự như vậy.
Ví dụ: khoảng trắng trong một chuỗi được mã hoá bằng %20 hoặc được thay thế bằng dấu cộng (+). Nếu bạn sử dụng ký tự gạch thẳng (|) làm dấu phân cách, hãy nhớ mã hoá gạch thẳng thành %7C. Dấu phẩy trong chuỗi phải được mã hoá thành %2C.
Bạn nên sử dụng các thư viện tạo URL thông thường của nền tảng để tự động mã hoá URL, nhằm đảm bảo URL được thoát đúng cách cho nền tảng của bạn.
Tạo URL hợp lệ
Có thể bạn nghĩ rằng một URL "hợp lệ" là điều hiển nhiên, nhưng không hẳn như vậy. Ví dụ: URL được nhập vào thanh địa chỉ trong trình duyệt có thể chứa các ký tự đặc biệt (ví dụ: "上海+中國"); trình duyệt cần dịch nội bộ những ký tự đó thành một phương thức mã hoá khác trước khi truyền.
Tương tự, mọi mã tạo hoặc chấp nhận dữ liệu đầu vào UTF-8 đều có thể coi URL có ký tự UTF-8 là "hợp lệ", nhưng cũng cần dịch các ký tự đó trước khi gửi đến một máy chủ web.
Quá trình này được gọi là
mã hoá URL hoặc mã hoá bằng dấu phần trăm.
Các ký tự đặc biệt
Chúng ta cần dịch các ký tự đặc biệt vì tất cả URL đều phải tuân thủ cú pháp do quy cách Giá trị nhận dạng tài nguyên đồng nhất (URI) chỉ định. Trên thực tế, điều này có nghĩa là URL chỉ được chứa một tập hợp con đặc biệt của các ký tự ASCII: các ký hiệu chữ và số quen thuộc và một số ký tự dành riêng để sử dụng làm ký tự điều khiển trong URL. Bảng này tóm tắt các ký tự này:
Tóm tắt các ký tự hợp lệ trong URL
Đặt
ký tự
Cách sử dụng URL
Chữ và số
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
Chuỗi văn bản, cách sử dụng lược đồ (http), cổng (8080), v.v.
Không dành riêng
- _ . ~
Chuỗi văn bản
Đã đặt trước
! * ' ( ) ; : @ & = + $ , / ? % # [ ]
Ký tự điều khiển và/hoặc Chuỗi văn bản
Khi tạo một URL hợp lệ, bạn phải đảm bảo rằng URL đó chỉ chứa những ký tự xuất hiện trong bảng. Việc điều chỉnh một URL để sử dụng bộ ký tự này thường dẫn đến 2 vấn đề, một vấn đề về việc bỏ sót và một vấn đề về việc thay thế:
Các ký tự mà bạn muốn xử lý nằm ngoài bộ ký tự nêu trên. Ví dụ: các ký tự bằng ngôn ngữ nước ngoài (chẳng hạn như 上海+中國) cần được mã hoá bằng các ký tự ở trên. Theo quy ước phổ biến, khoảng trắng (không được phép xuất hiện trong URL) cũng thường được biểu thị bằng ký tự dấu cộng '+'.
Các ký tự tồn tại trong bộ ký tự nêu trên dưới dạng ký tự dành riêng, nhưng cần được sử dụng theo đúng nghĩa đen.
Ví dụ: ? được dùng trong URL để cho biết phần đầu của chuỗi truy vấn; nếu muốn dùng chuỗi "? and the Mysterions", bạn cần mã hoá ký tự '?'.
Tất cả các ký tự cần được mã hoá URL đều được mã hoá bằng ký tự '%' và giá trị hex gồm 2 ký tự tương ứng với ký tự UTF-8 của chúng. Ví dụ: 上海+中國 trong UTF-8 sẽ được mã hoá thành URL dưới dạng %E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B. Chuỗi ? and the Mysterians sẽ được mã hoá URL thành %3F+and+the+Mysterians hoặc %3F%20and%20the%20Mysterians.
Các ký tự phổ biến cần mã hoá
Sau đây là một số ký tự phổ biến phải được mã hoá:
Ký tự không an toàn
Giá trị được mã hoá
Không gian
%20
"
%22
<
%3C
>
%3E
#
%23
%
%25
|
%7C
Đôi khi, việc chuyển đổi một URL mà bạn nhận được từ dữ liệu đầu vào của người dùng sẽ rất khó khăn. Ví dụ: người dùng có thể nhập địa chỉ là "5th&Main St."
Thông thường, bạn nên tạo URL từ các phần của URL đó, coi mọi dữ liệu đầu vào của người dùng là ký tự nguyên nghĩa.
Ngoài ra, URL bị giới hạn ở 16384 ký tự đối với tất cả các dịch vụ web và API web tĩnh của Nền tảng Google Maps. Đối với hầu hết các dịch vụ, bạn hiếm khi đạt đến giới hạn ký tự này. Tuy nhiên, xin lưu ý rằng một số dịch vụ có nhiều tham số có thể dẫn đến URL dài.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-05 UTC."],[[["\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."]]