How Slugification Works: From Title to URL-Safe String

May 28, 20265 min read

What Is Slugification?

Slugification is the process of converting a human-readable title into a URL-safe string. It transforms text like "How to Build a REST API" into how-to-build-a-rest-api.

The algorithm strips away anything a URL cannot safely carry: special characters, accents, spaces, and non-ASCII text. What remains is a lowercase, hyphen-separated string that works in any browser.

The Slugification Pipeline

Slugification follows a predictable sequence of steps. Each step narrows the input until only URL-safe characters remain.

Step 1: Unicode Normalization (NFKD)

The input text is first normalized using NFKD (Normalization Form Compatibility Decomposition). This step separates base characters from their combining marks.

é → e + ◌́  (LATIN SMALL LETTER E + COMBINING ACUTE ACCENT)
ü → u + ◌̈  (LATIN SMALL LETTER U + COMBINING DIAERESIS)

NFKD is preferred over NFC because it decomposes characters, making the next step — stripping diacritics — possible.

Step 2: Remove Diacritics

After decomposition, the combining marks are stripped. This converts accented characters to their closest ASCII equivalent.

InputAfter NFKDAfter Stripping
cafécafe + ◌́cafe
überuber + ◌̈uber
piñatapinata + ◌̃pinata
naïvenai + ◌̈ + venaive

Step 3: Remove Non-Word Characters

Any character that is not a letter, digit, or space is removed. This eliminates punctuation, symbols, and other special characters.

"How to: Build an API!" → "How to Build an API"
"Price: $9.99 (50% off)" → "Price 999 50 off"
"C++ & Python" → "C Python"

Step 4: Replace Spaces With Separators

Spaces are converted to the chosen separator — typically a hyphen (-). This creates word boundaries in the slug.

"How to Build an API" → "How-to-Build-an-API"

Step 5: Collapse Multiple Separators

Consecutive separators are reduced to a single instance. This happens when the original text had multiple spaces or when removed characters left gaps.

"C  Python" → "C---Python" → "C-Python"

Step 6: Convert to Lowercase and Trim

The final step lowercases everything and removes leading and trailing separators.

"How-to-Build-an-API" → "how-to-build-an-api"

The Unicode Challenge

Slugification works cleanly for Latin scripts. For other writing systems, the results vary:

InputSlugIssue
こんにちは`` (empty)Japanese characters have no ASCII mapping
Привет мир`` (empty)Cyrillic characters are stripped entirely
你好世界`` (empty)Chinese characters decompose to nothing
café résumécafe-resumeLatin diacritics convert cleanly

For non-Latin content, you have three options:

  • Transliterate. Use a library that maps characters to phonetic ASCII equivalents: こんにちはkonnichiha, Приветprivet.
  • Use the raw Unicode. Some systems allow Unicode slugs (/こんにちは), but this hurts readability and URL sharing.
  • Generate a separate slug manually. Write a descriptive ASCII slug instead of auto-generating from the title.

Slugify Libraries Compared

Most programming ecosystems have slugify libraries. Here is a comparison of the popular options:

LibraryLanguageUnicode HandlingCustom SeparatorTransliteration
python-slugifyPythonNFKD + diacritics stripYesBuilt-in
slugifyJavaScript (npm)NFKD + diacritics stripYesOptional
Laravel Str::slugPHPNFKD + diacritics stripYesVia stringy
Go slugGoNFKD + diacritics stripYesVia transliteration map
Rails ActiveSupport::ParameterizeRubyNFKD + diacritics stripYesLimited

Python example

from slugify import slugify

slugify("How to Build a REST API")
# "how-to-build-a-rest-api"

slugify("Café résumé: 10 tips")
# "cafe-resume-10-tips"

slugify("こんにちは世界", transliterate=True)
# "konnichiha-shi-jie"

JavaScript example

import slugify from 'slugify';

slugify('How to Build a REST API');
// "How-to-Build-a-REST-API"

slugify('How to Build a REST API', { lower: true });
// "how-to-build-a-rest-api"

slugify('Café résumé: 10 tips');
// "Cafe-resume-10-tips"

Choosing a Separator

Three separators are commonly used in URLs:

SeparatorExampleSEO ImpactReadability
Hyphen -/best-seo-tipsGoogle recognizes as word separatorHigh
Underscore _/best_seo_tipsGoogle does not treat as word separatorMedium
Dot ./best.seo.tipsAmbiguous — can indicate file extensionsLow

Use hyphens. Google's John Mueller has confirmed that hyphens are the preferred URL separator. Underscores cause Google to join words together, so best_seo_tips is read as bestseotips.

Key Takeaways

  • Slugification converts titles into URL-safe strings through a six-step pipeline
  • NFKD normalization decomposes characters so diacritics can be stripped
  • Non-Latin scripts produce empty slugs unless transliteration is applied
  • Always use hyphens as separators — Google treats underscores as word joiners
  • Most languages have mature slugify libraries that handle the full pipeline
  • Test your slugify function with accented input, symbols, and non-Latin text

Try It Yourself

Turn any title into a clean slug instantly with our Slug Generator, and convert text between naming formats with the Case Converter — both free, no account needed.