Slug Generation for SEO: A Complete Guide
Clean URLs are an SEO ranking factor and a usability win. Here's the slug-generation rules that hold up across languages, databases, and search engines.
By Syed Husnain Haider Bukhari · · Updated
A URL slug is the human-readable part of a URL — the `how-to-bake-bread` in `example.com/blog/how-to-bake-bread`. Slugs matter because they appear in search results, get shared on social media, anchor backlinks, and quietly nudge click-through rates. A clean slug like `/pricing-plans` outperforms an opaque one like `/page?id=4827` on every measurable axis.
This guide covers what makes a good slug, the rules for generating them from arbitrary input strings, and the edge cases — Unicode, uniqueness, length limits — that trip up naive slug generators.
What a good slug looks like
A good slug is lowercase, uses hyphens to separate words (never underscores or spaces), contains only ASCII letters, digits, and hyphens, and conveys the page's topic in 3–5 words. `/best-running-shoes-2026` does its job. `/article_4827_v2_FINAL` does not.
Search engines, including Google, have stated that hyphens are word separators in URLs while underscores are not. `best_running_shoes` is read as one token (`bestrunningshoes`), which fails to match queries for the individual words. Always hyphenate.
Length and stop words
Shorter is better, with a soft target around 3–6 meaningful words and 60 characters maximum. Long slugs get truncated in search snippets and are awkward to share verbally ("go to slash how-to-make-the-best-chocolate-chip-cookies-from-scratch-easy-recipe").
Removing stop words (a, an, the, of, in, on, etc.) shortens slugs without losing meaning. `how-to-bake-the-best-bread` becomes `bake-best-bread`. Be careful with aggressive removal — `to be or not to be` becomes nothing useful. Use a moderate stop-word list and keep the slug recognizable to a human.
Transliterating non-ASCII characters
Pure ASCII slugs travel best — they survive copy-paste, work in every URL parser, and don't trigger Punycode warnings in some clients. The transformation from Unicode to ASCII is called transliteration: `naïve` becomes `naive`, `Žížala` becomes `zizala`, `北京` becomes `bei-jing` (pinyin) or `beijing`.
JavaScript's `string.normalize('NFD').replace(/[\u0300-\u036f]/g, '')` strips combining diacritical marks, handling most Latin-script transliteration in one line. For Cyrillic, Greek, Arabic, CJK, and other scripts, use a library like `slugify` or `transliteration` that knows the per-script conventions.
Stripping punctuation and normalizing whitespace
After lowercasing and transliterating, replace every character that isn't a letter, digit, or hyphen with a hyphen, then collapse runs of hyphens into a single hyphen, then trim leading/trailing hyphens. `"Hello, World! Welcome to 2026."` becomes `hello-world-welcome-to-2026`.
Decide whether digits are allowed in slugs. They usually are — years, model numbers, version numbers all benefit from being in the URL — but some style guides forbid them in favor of spelled-out forms. Be consistent.
Uniqueness in the database
Two different articles can produce identical slugs. "My First Post" written by Alice and "My First Post" written by Bob both slugify to `my-first-post`. The standard fix is to append a counter on collision: `my-first-post`, `my-first-post-2`, `my-first-post-3`.
Another option is to prefix with the record ID: `4827-my-first-post`. This guarantees uniqueness without lookups and is faster to query, at the cost of less-clean URLs. For high-write platforms (Medium, Reddit, Hacker News) the ID-prefix approach is common.
Whatever you choose, store the slug as a separate database column with a unique constraint. Don't compute it on every request — generate at write time and persist.
URL changes and redirects
Once a slug is published and indexed by Google, changing it has costs. Internal links break, backlinks 404, and accumulated PageRank takes weeks to migrate to the new URL. If you must change a slug, always set up a 301 redirect from the old URL to the new one, and keep that redirect indefinitely.
The safer practice is to make slugs immutable. If the title of an article changes, leave the slug alone. The slug doesn't need to perfectly match the current title — it just needs to be a stable, descriptive identifier.
Slug-generation checklist
- Lowercase the input.
- Transliterate non-ASCII characters (or keep them, if your audience expects native script).
- Replace whitespace and disallowed characters with hyphens.
- Collapse repeated hyphens into one. Trim leading/trailing hyphens.
- Truncate to ~60 characters at a word boundary.
- Ensure uniqueness with a counter suffix or ID prefix on collision.
- Store the slug as a unique-indexed column; treat it as immutable after publish.
Wrapping up
Slugs are tiny but high-leverage. Good ones improve SEO, click-through rates, and shareability; bad ones tag your site as auto-generated junk. The rules are easy: lowercase, hyphens, ASCII, short, descriptive, unique, immutable.
Our free slug generator handles transliteration, stop-word removal, and length truncation in one step. Paste a title, get a clean slug, copy it into your CMS. Useful for bulk migrations and quick previews alike.