Turn a title into a URL slug
A headline typed into a CMS arrives with curly quotes, an em dash and sometimes an invisible character from wherever it was pasted. The recipe produces a display title in consistent case and a slug that is safe in a URL.
The steps
- 1
Clean hidden charactersvia Invisible Character Detector
Straightens smart quotes, removes zero-width characters and non-breaking spaces.
- 2
Change casevia Case Converter
Title case for the display version; small words stay lower-case.
- 3
Slugifyvia Slug / URL Encoder
Lower-case ASCII, words joined by single hyphens, punctuation dropped, accents removed.
What a good slug looks like
Lower-case, ASCII only, words separated by single hyphens, no leading or trailing hyphen, no punctuation. Accented letters are transliterated rather than dropped, so "café" becomes "cafe" rather than "caf". Numbers are kept, which matters for years and versions, though the dot inside a version is dropped rather than hyphenated: the sample becomes ten-things-we-learned-shipping-v20-in-q3.
Why the cleaning step is not optional
A zero-width space in a headline is invisible in every editor and survives into the slug as an encoded sequence, producing a URL that looks right and does not match the one in the sitemap. The cleaning step exists because that has happened to most people who run a site for long enough. Straightening quotes also keeps the display title consistent across articles.
Title case rules
The first and last words are capitalised, as are all others except a short list of articles, conjunctions and prepositions. Words that are already in all capitals, such as an acronym, are left as they are. This is one common style among several; a house style that differs is easy to apply by hand on the display title, and does not affect the slug.
Questions
- Can I keep underscores instead of hyphens?
- The slug tool has a separator option; open it from the last step. Hyphens are the convention because search engines treat them as word breaks and underscores as joiners.
- What about non-Latin scripts?
- Characters without an ASCII transliteration are dropped, which can leave the slug empty for a title entirely in another script. Translate the title or write the slug by hand in that case.