Last updated
What Is a URL Slug?
A URL slug is the human-readable part of a URL that identifies a specific page. A blog post titled "10 Best Practices for REST API Design" gets the slug 10-best-practices-for-rest-api-design. Good slugs are lowercase, hyphen-separated, keyword-rich, and free of special characters.
Basic Slug Generation
Input title: "Introduction to Machine Learning with Python"
Transformations applied:
1. Lowercase: introduction to machine learning with python
2. Replace spaces: introduction-to-machine-learning-with-python
3. Remove special chars: (none to remove)
4. Collapse hyphens: (no consecutive hyphens)
5. Trim hyphens: (no leading/trailing hyphens)
Output slug: introduction-to-machine-learning-with-python
Handling Special Characters
Input: "C++ vs. Rust: Which Language Should You Choose in 2024?"
Step-by-step:
Lowercase: c++ vs. rust: which language should you choose in 2024?
Transliterate: c++ vs. rust: which language should you choose in 2024?
Remove special: c vs rust which language should you choose in 2024
Replace spaces: c--vs--rust--which-language-should-you-choose-in-2024
Collapse hyphens: c-vs-rust-which-language-should-you-choose-in-2024
Output: c-vs-rust-which-language-should-you-choose-in-2024
Diacritic Transliteration
Accented characters are converted to their ASCII equivalents:
Input: "Café au Lait: Recette Française Authentique"
Transliteration:
é → e
è → e
ç → c
â → a
ô → o
û → u
ñ → n
ü → u
ö → o
ä → a
Output: cafe-au-lait-recette-francaise-authentique
More examples:
"Über die Straße" → uber-die-strasse
"Año Nuevo" → ano-nuevo
"Ångström Unit" → angstrom-unit
"Søren Kierkegaard" → soren-kierkegaard
Stop Word Removal (SEO Optimization)
Removing common stop words creates shorter, more keyword-dense slugs:
Input: "The Complete Guide to Building REST APIs with Node.js"
Without stop word removal:
the-complete-guide-to-building-rest-apis-with-node-js
(52 characters)
With stop word removal (removes: the, to, with):
complete-guide-building-rest-apis-node-js
(41 characters — shorter and more keyword-dense)
Common stop words removed:
a, an, the, and, or, but, in, on, at, to, for,
of, with, by, from, is, are, was, were, be, been
Maximum Length Control
Input: "A Comprehensive Deep Dive into the Architecture of Modern Distributed Database Systems and Their Performance Characteristics"
Full slug (too long):
a-comprehensive-deep-dive-into-the-architecture-of-modern-distributed-database-systems-and-their-performance-characteristics
(124 characters)
Truncated to 60 characters (at word boundary):
comprehensive-deep-dive-architecture-modern-distributed
(55 characters — truncated cleanly at word boundary)
Recommended max length: 60–70 characters for SEO
Hyphens vs Underscores
Input: "JavaScript Array Methods"
With hyphens (recommended):
javascript-array-methods
Google treats hyphens as word separators
"javascript", "array", "methods" are separate keywords ✓
With underscores (not recommended for SEO):
javascript_array_methods
Google treats underscores as word joiners
"javascript_array_methods" is treated as ONE keyword ✗
Verdict: Always use hyphens for SEO-friendly slugs.
Handling Numbers
Input: "15 JavaScript Tips for 2024"
Output: 15-javascript-tips-2024
(numbers preserved — they carry meaning)
Input: "Top 10 Python Libraries for Data Science"
Output: top-10-python-libraries-data-science
(with stop word removal: "for" removed)
Full URL Preview
Domain: https://myblog.com/posts/
Title: "Getting Started with Docker and Kubernetes"
Generated slug: getting-started-docker-kubernetes
Full URL preview:
https://myblog.com/posts/getting-started-docker-kubernetes
How it appears in search results:
myblog.com › posts › getting-started-docker-kubernetes
Batch Slug Generation
Input titles:
"How to Use Git Rebase"
"Understanding Async/Await in JavaScript"
"Docker Compose: A Practical Guide"
"Building REST APIs with FastAPI"
Generated slugs:
how-to-use-git-rebase
understanding-async-await-javascript
docker-compose-practical-guide
building-rest-apis-fastapi
Slug Generation in Code
// JavaScript
function generateSlug(title) {
return title
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '') // remove diacritics
.replace(/[^a-z0-9\s-]/g, '') // remove special chars
.trim()
.replace(/\s+/g, '-') // spaces to hyphens
.replace(/-+/g, '-'); // collapse hyphens
}
generateSlug("Café au Lait: Recette Française");
// → "cafe-au-lait-recette-francaise"
// Python
import re
import unicodedata
def generate_slug(title):
title = unicodedata.normalize('NFD', title.lower())
title = ''.join(c for c in title if unicodedata.category(c) != 'Mn')
title = re.sub(r'[^a-z0-9\s-]', '', title)
title = re.sub(r'\s+', '-', title.strip())
return re.sub(r'-+', '-', title)
SEO Best Practices for Slugs
- Keep slugs under 60–70 characters
- Use hyphens, not underscores
- Include the primary keyword near the beginning
- Avoid stop words (the, a, an, and, or, in, to)
- Use only lowercase letters, numbers, and hyphens
- Never change a published slug — it breaks existing links and SEO
- Use 301 redirects if you must change a slug