Sub topic 5
Handling special characters and non-English text
This chapter assumes you know what a slug is and how to write a basic one: lowercase, hyphen-separated, short. It also assumes you have read a URL with percent signs in it before. Here you will see what happens when a title contains characters outside a-z and 0-9.
Which characters are safe in a slug
Stick to lowercase letters, digits, and hyphens. Other characters cause trouble for specific reasons:
| Character | Problem | Better choice |
|---|---|---|
| Space | Becomes `%20` in the URL | Hyphen |
| Underscore | Search engines may treat `seo_tips` as one word, while `seo-tips` is two | Hyphen |
| `&`, `?`, `#`, `/` | Reserved characters. They change the URL's structure (query, fragment, path) | Remove, or write "and" |
| Uppercase letters | `/About` and `/about` can be separate URLs on many servers | Lowercase only |
| Accents, such as `é` | Must be percent-encoded | Transliterate to `e` |
Percent-encoding
URLs can carry only a limited set of ASCII characters. Anything else is converted to its UTF-8 bytes, and each byte is written as % plus two hex digits. This is percent-encoding. The browser may show a readable address, but the underlying URL is encoded.
from urllib.parse import quote, unquote
print(quote("crème-brûlée")) # cr%C3%A8me-br%C3%BBl%C3%A9e
print(quote("東京")) # %E6%9D%B1%E4%BA%AC
print(unquote("%E6%9D%B1%E4%BA%AC")) # 東京Encoded slugs work, but they are hard to read in logs, emails, and plain-text copies. They are also easy to mismatch: if one link uses %c3%a8 (lowercase hex) and another uses %C3%A8, your tools may treat them as different URLs.
Two strategies for non-English text
Which one you choose depends on your audience.
flowchart TDReplace that sketch with the real diagram below.
- Transliterate. Convert
étoe,ütou. This works well for French, Spanish, and Portuguese. - Keep the native script. For Japanese, Chinese, Arabic, or Russian, readers recognize their own script, and Google handles UTF-8 URLs. Use it when your audience reads that language, and apply the same encoding everywhere.
Whichever you pick, apply it to the whole site. Mixing both for the same language creates inconsistent URLs.
Transliteration in code
This function decomposes accented letters and drops what ASCII cannot hold.
import re
import unicodedata
def slugify(title, max_length=60):
text = unicodedata.normalize("NFKD", title).encode("ascii", "ignore").decode("ascii")
text = text.lower()
text = re.sub(r"[^a-z0-9]+", "-", text).strip("-")
return text[:max_length].rstrip("-")
print(slugify("Crème Brûlée Recipe: Easy & Quick!")) # creme-brulee-recipe-easy-quick
print(slugify("Straße")) # strae
print(slugify("東京")) # (empty string)The last two lines show the limits of this approach:
- Letters with no Unicode decomposition, such as
ß,ø, andł, are dropped, not converted.Straßebecomesstrae, which is wrong. - Scripts with no Latin equivalent produce an empty slug.
For German and similar languages, replace special letters before normalizing, for example ß to ss. A transliteration library such as python-slugify or Unidecode covers more cases than a hand-written map.
Common errors
- Mixing encoded and unencoded versions of the same URL in internal links.
- Leaving a trailing
%20or double hyphen after stripping punctuation. - Letting a CMS auto-generate slugs from non-English titles without review.
- Changing the slug strategy later without 301 redirects, which breaks existing links.
Exercise
Your Python slug function turns the title "Åre skiing guide" into "re-skiing-guide" and "Łódź travel tips" into "odz-travel-tips". Why do the first letters disappear, and what would you do about it?
Show answer
Å decomposes under NFKD into A plus a combining ring, so it should survive as a. If the first letter vanished, the input likely used a precomposed form that was not normalized, or a different character looked like Å. For Ł, there is no decomposition, so the ASCII step drops it entirely. Fix this by adding an explicit replacement map (Ł to L, ß to ss) before normalizing, or by using a transliteration library, and review the output for each language you publish in.