Guides Web & HTTP

How URLs and Query Strings Work

The seven parts of a URL, which characters need percent-encoding and why, how query strings carry repeated keys, and how international domain names become punycode.

Last reviewed:

AdSense Placeholder
Slot: header_reference_page
On this page

A URL (uniform resource locator) is an address with up to seven parts. The part after the ? is the query string, a list of key=value pairs that passes small amounts of data to the server. Because a few characters have special meaning inside a URL, anything else that contains them must be percent-encoded.

Anatomy of a URL

Take this address:

https://[email protected]:8443/shop/shoes?q=red+shoes&size=9&tag=a&tag=b#reviews
Part Value Notes
Scheme https Which protocol to use
Userinfo user Rare, and best avoided
Host www.example.com Which server; case-insensitive
Port 8443 Defaults: 80 for HTTP, 443 for HTTPS
Path /shop/shoes Which resource on the server
Query q=red+shoes&size=9&tag=a&tag=b Parameters, after ?
Fragment reviews A spot within the page; never sent to the server

Paste any address into the URL Parser to see these parts and every query parameter listed separately.

Percent-encoding

Reserved characters have jobs in a URL: ? starts the query, & separates pairs, = separates key from value, # starts the fragment, / separates path segments. To use one as plain data, write % followed by its byte value in hexadecimal. Characters outside ASCII are first turned into UTF-8 bytes, and each byte is encoded.

Character Encoded
space %20 (or + in form-encoded queries)
# %23
& %26
= %3D
? %3F
/ %2F
% %25
é %C3%A9
€ %E2%82%AC

Encode and decode text with the URL Encode and URL Decode. Note that % itself must be encoded as %25, which is why decoding twice breaks values.

Query strings

Pairs are separated by &. The same key can repeat, and receivers decide how to read it. Parsing the query above gives:

Key Values
q red shoes
size 9
tag a, b (the key appears twice)

To build a correctly encoded query, enter the pairs into the Query String Builder. For example, the values red shoes & boots and 10-20 become q=red+shoes+%26+boots&price=10-20. Campaign tags are an application of the same idea: see How to Build UTM Links for Campaign Tracking.

International domain names

Hostnames travel as ASCII. A name with accented or non-Latin letters is converted to punycode and prefixed with xn--: münchen.de becomes xn--mnchen-3ya.de. Look-alike letters from other alphabets make the same mechanism a favourite of phishers, so convert suspicious hosts with the Hostname to Punycode Converter.

Common pitfalls

  • Forgetting to encode a value. An unescaped & or # in a value silently cuts it off.
  • Double encoding. Encoding an already-encoded string turns %20 into %2520.
  • Putting secrets in the URL. Query strings end up in logs, history and referrer headers.
  • Assuming one value per key. Read repeated keys as lists.
  • Trusting the visible part of a link. See How to Analyze a Suspicious Link Safely.

Try these tools

See also

  • Glossary URL
    A URL (Uniform Resource Locator) is the address of a resource on the web, such as a page, image or API endpoint.
  • Glossary Query string
    A query string is the part of a URL after the question mark that carries named values for the server, such as ?q=shoes&page=2.
  • Glossary Punycode
    Punycode is the encoding that lets domain names with non-ASCII characters be stored in the ASCII-only DNS, marked by the prefix xn--.
  • Glossary HTTP
    HTTP (Hypertext Transfer Protocol) is the protocol browsers and servers use to request and send web pages, files and data.
  • Glossary Domain name
    A domain name is the human-readable address of a website or service, such as example.com, which DNS translates into an IP address.
  • Glossary Percent-encoding
    Percent-encoding writes a character that is not allowed in a URL, or has a special meaning there.
  • Glossary UTM parameters
    UTM parameters are tags added to the end of a URL, such as utm_source and utm_campaign.

Frequently Asked Questions

In the path of a URL a space must be written as %20. In a query string submitted by an HTML form, the older convention writes it as +. Servers decode both inside a query, but only %20 is safe everywhere.

No. The fragment is handled by the browser and is not sent in the request, so it cannot be used for server-side decisions.

The scheme and host are not, but the path usually is, and so are query values on most servers. /Shop and /shop can be different pages.

AdSense Placeholder
Slot: footer_leaderboard