Computers store text as numbers. A character encoding is the agreed rule for turning each character into bytes, and back again. When a browser, crawler or database reads text with the same rule that wrote it, you see "café". When it uses a different rule, you see "café".
Character set vs encoding: Unicode and UTF-8
Two ideas are easy to mix up:
- A character set gives every character a number, called a code point. Unicode is the character set the modern world uses. It covers nearly every writing system plus emoji. The letter A is U+0041, é is U+00E9, and 🦙 is U+1F999.
- An encoding says how to write those numbers as bytes. UTF-8, UTF-16 and UTF-32 are three encodings of Unicode.
UTF-8 uses one to four bytes per character. The 128 ASCII characters (English letters, digits, basic punctuation) take one byte each, with the same values ASCII always had, so a plain ASCII file is already valid UTF-8. Other characters take more bytes: é is two bytes (C3 A9), most Chinese characters three, emoji four. RFC 3629
Before Unicode, each region had its own one-byte encoding, such as ISO-8859-1 or Windows-1252 for Western Europe and Windows-1251 for Cyrillic. Each could hold only 256 characters, so no single page could mix scripts. You still meet these in old systems, exports and emails.
On the web, the answer is UTF-8
The HTML standard settles the choice: an HTML document must be encoded in UTF-8. WHATWG HTML Search engines expect it too. Google requires UTF-8 for robots.txt files and for sitemaps.
UTF-16 and UTF-32 have their uses inside programs, but not in files you serve. They take more bytes for most web text and bring byte-order questions that UTF-8 doesn't have.
How to declare UTF-8
A browser can't reliably guess an encoding from the bytes, so tell it. Do both of these:
- Send it in the HTTP header. Your server should answer HTML requests with
Content-Type: text/html; charset=utf-8. - Put it in the HTML. Add the meta tag as the first thing in
<head>:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Café menu</title>
The tag must sit within the first 1024 bytes of the file, so keep it above long comments, scripts and styles. Use only one such tag per page.
Then make sure the file really is UTF-8. A declaration doesn't convert anything: if your editor saved the file as Windows-1252 and the tag says UTF-8, the page still breaks. Most editors show the file's encoding in the status bar and let you save as UTF-8.
Fixing garbled text (mojibake)
Garbled text has a name: mojibake. The pattern tells you what went wrong.
| You see | What happened | Where to look |
|---|---|---|
café, ’ instead of ’ |
UTF-8 bytes read as Windows-1252 or ISO-8859-1 | A missing or wrong charset in the header or meta tag, or a database connection not set to UTF-8 |
caf� (a diamond with a question mark) |
Bytes that aren't valid UTF-8, often Windows-1252 text read as UTF-8 | An old file or data import that was never converted |
 at the very top of a page or file |
A UTF-8 byte order mark (BOM) read as text | Save the file as UTF-8 without BOM |
| Empty boxes for some characters | The encoding is fine; the font has no shape for that character | Choose a font that covers the script |
Fix the cause, not the output. Find the first place where text was read with the wrong rule (the file, the database, the connection, the HTTP header) and correct it there. Converting text from one encoding to another, called transcoding, fixes old data. Running a converter twice garbles it again.
What encoding means for SEO
Search engines read your text the same way browsers do. If a title or product name arrives garbled, the garbled version can show up in search results, and the words people search for may not match what was indexed.
Three files deserve a check:
- robots.txt must be UTF-8. Google may ignore characters outside UTF-8, and treats a byte order mark as an invalid line it skips. Google Search Central
- XML sitemaps must be UTF-8 encoded.
- URLs with non-ASCII characters should be percent-encoded from their UTF-8 bytes in your links. Google's own example:
gemüsebecomesgem%C3%BCse. See URL encoding for the rules, and Punycode for non-ASCII domain names.
HTML entities such as é still work, but with UTF-8 you don't need them for ordinary letters. Type the character itself; keep entities for characters that have a meaning in HTML markup, like < and &.