Slug Generator
Convert any text into a clean URL-friendly slug
Input text
Slug
What Is a URL Slug?
A URL slug is the part of a web address that identifies a specific page in a human-readable form. In the URL https://example.com/blog/my-first-post, the slug is my-first-post. A slug generator transforms a page title or description into a string that contains only lowercase letters, numbers, and hyphens (or another chosen separator). This process removes spaces, strips accents and diacritics, drops special characters, and normalizes whitespace into single separators.
Slug generation is a standard step in content management systems, static site generators, blog platforms, and any application that derives URLs from user input. WordPress, Ghost, Hugo, Next.js, and Django all include slug generation logic because readable URLs improve both usability and search engine optimization. A well-formed slug tells the user what a page contains before they click the link.
The term "slug" comes from newspaper publishing, where it referred to a short label used to identify a story during production. In web development, the slug performs the same function: it is a compact, unique, URL-safe identifier derived from a longer title or name. Because slugs are derived programmatically, a reliable slug generator ensures consistency across every page and locale.
Why Use This Slug Generator?
Manually creating URL slugs is error-prone. Forgetting to strip an accent, leaving a double hyphen, or missing an edge case with Unicode input can produce broken or ugly URLs. This tool handles those problems automatically.
Slug Generator Use Cases
Slug Generation Rules and Character Handling
Slug generation follows a predictable sequence of transformations. Understanding each step helps you debug unexpected output or build your own slugify function.
- 1. Unicode Normalization (NFD)Decompose combined characters into base character + combining mark. For example, é (U+00E9) becomes e + combining acute accent (U+0301). This makes diacritics removable in the next step.
- 2. Strip DiacriticsRemove all characters in the Unicode Combining Diacritical Marks block (U+0300–U+036F). After this step, café becomes cafe and Ñ becomes N.
- 3. Remove Special CharactersReplace any character that is not a letter, digit, whitespace, or hyphen with a space. This drops punctuation, symbols, and characters that have no ASCII equivalent.
- 4. Trim and Collapse WhitespaceRemove leading and trailing whitespace, then collapse all runs of consecutive spaces, underscores, or hyphens into a single chosen separator.
- 5. Apply Case and SeparatorConvert to lowercase (optional) and join words with the chosen separator character: hyphen (-), underscore (_), or dot (.).
Character Transformation Reference
The table below shows how common characters are handled during slug generation:
| Input | Output | Rule Applied |
|---|---|---|
| Hello World | hello-world | Lowercase + space → hyphen |
| Café au Lait | cafe-au-lait | NFD normalization strips é → e |
| naïve résumé | naive-resume | Multiple diacritics removed |
| Price: $9.99! | price-9-99 | Symbols ($, !, :) removed |
| too many | too-many | Whitespace trimmed and collapsed |
| one--two___three | one-two-three | Mixed separators collapsed |
| Привет мир | privet-mir | Cyrillic (if transliteration) or removed |
| file_name.txt | file-name-txt | Dots and underscores replaced |
| React & Vue | react-vue | Ampersand removed |
| 2026-03-30 | 2026-03-30 | Digits and hyphens preserved |
Code Examples
Slug generation in popular languages and frameworks. Each example handles Unicode normalization, diacritic removal, and separator insertion.
function slugify(text, separator = '-') {
return text
.normalize('NFD') // decompose accented chars
.replace(/[\u0300-\u036f]/g, '') // strip diacritics
.toLowerCase()
.replace(/[^\w\s-]/g, ' ') // drop special chars
.trim()
.replace(/[\s_-]+/g, separator) // collapse whitespace → separator
// slugify('Café au Lait') → "cafe-au-lait"
// slugify('Hello World', '_') → "hello_world"
}
// Node.js alternative using the `slugify` npm package:
// npm install slugify
// const slugify = require('slugify')
// slugify('Hello World', { lower: true, strict: true }) → "hello-world"import re
import unicodedata
def slugify(text: str, separator: str = '-') -> str:
"""Convert text to a URL-safe slug."""
text = unicodedata.normalize('NFD', text)
text = text.encode('ascii', 'ignore').decode('ascii') # strip non-ASCII
text = text.lower()
text = re.sub(r'[^\w\s-]', ' ', text)
text = text.strip()
text = re.sub(r'[\s_-]+', separator, text)
return text
# slugify('Café au Lait') → "cafe-au-lait"
# slugify('Hello World', '_') → "hello_world"
# Alternative: python-slugify (pip install python-slugify)
# from slugify import slugify
# slugify('Café au Lait') → "cafe-au-lait"package main
import (
"regexp"
"strings"
"unicode"
"golang.org/x/text/unicode/norm"
"golang.org/x/text/transform"
"golang.org/x/text/runes"
)
func slugify(text string) string {
// NFD normalize and strip diacritics
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ := transform.String(t, text)
result = strings.ToLower(result)
re := regexp.MustCompile(`[^\w\s-]+`)
result = re.ReplaceAllString(result, " ")
result = strings.TrimSpace(result)
re = regexp.MustCompile(`[\s_-]+`)
result = re.ReplaceAllString(result, "-")
return result
}
// slugify("Café au Lait") → "cafe-au-lait"
// slugify("Hello World") → "hello-world"function slugify(string $text, string $separator = '-'): string {
// Transliterate non-ASCII characters
$text = transliterator_transliterate(
'Any-Latin; Latin-ASCII; Lower()', $text
);
// Remove anything that is not a word char, space, or hyphen
$text = preg_replace('/[^\w\s-]/', ' ', $text);
$text = trim($text);
$text = preg_replace('/[\s_-]+/', $separator, $text);
return $text;
}
// slugify('Café au Lait') → "cafe-au-lait"
// slugify('Hello World', '_') → "hello_world"