Sub topic 4
Writing good slugs
This chapter assumes you know what a slug is and where it sits in a URL, as covered earlier. It also assumes you can read basic Python and HTML. Here you will learn the rules for writing a slug, the reasons behind each rule, and how to automate the mechanical parts without producing broken slugs.
What a good slug does
A slug is the last readable segment of a URL path. In https://example.com/blog/url-slugs-guide/, the slug is url-slugs-guide. A good slug does three things:
- It tells a person what the page is about before they click.
- It gives search engines a small, clear hint about the topic.
- It stays valid for as long as the page exists.
The slug is a minor ranking signal. The title, headings, and content matter far more. The main reasons to care are readability, consistency, and avoiding later URL changes. Changing a slug after publication costs a redirect and some risk, so it is cheaper to write it well the first time.
The core rules
Use lowercase letters
Paths are case-sensitive on many servers. /Guide and /guide can be two different URLs, and each can serve the same content. That creates duplicate pages and broken links when someone types the wrong case. Lowercase everywhere removes the problem.
Separate words with hyphens
Use - between words, not _, spaces, or no separator at all. Google's documentation recommends hyphens because they read as word breaks. Underscores can join words together, so url_slugs may be read as one token. A space is encoded as %20, which makes the URL ugly and easy to mangle when pasted.
Keep it short and descriptive
Aim for two to five meaningful words. A slug should be a compact label for the page, not a copy of the whole title. Long slugs get truncated in search results and chat messages, and they are harder to read and remember.
Put the main topic in it
Use the words someone would use to describe the page. Take them from the page's focus, not from a keyword list. If the page is a guide to slugs, url-slugs-guide is clear. guide alone is too vague, and seo-url-slug-best-practices-tips-tricks-2026 is stuffed.
Drop filler words, but not meaning
Words like a, the, of, and and can usually go. Remove them only when the slug stays clear. how-to-write-slugs is fine as it is. Trimming it to write-slugs is acceptable. Trimming what-is-the-best-way-to-rename-a-page to best-rename-page loses the sense.
Avoid anything that will go stale
Dates, years, version numbers, and status words (new, latest, updated) make a slug wrong over time. A page at /blog/seo-tips-2019 looks outdated in 2026, and fixing it means a redirect. Use a date in the slug only when the date is part of the page's identity, such as an annual report or an event.
Do not carry state in the slug
Session IDs, sort orders, and tracking tags do not belong in a path. They create many URLs for one page. Keep identifiers meaningful and stable. If you need parameters for filtering or tracking, they belong in the query string, and the canonical tag (covered later) tells search engines which version is preferred.
Before and after
| Weak slug | Problem | Better slug |
|---|---|---|
| `/Blog/Post_123` | Mixed case, underscore, no meaning | `/blog/url-slugs-guide` |
| `/how-to-write-the-best-url-slugs-for-seo-in-2026-complete-guide` | Too long, dated, stuffed | `/blog/write-url-slugs` |
| `/p?id=48&ref=nav` | Parameters instead of a readable path | `/guides/anchor-text` |
| `/blog/seo-seo-seo-tips` | Repeated keyword | `/blog/seo-tips` |
| `/news/latest-update` | Will stop being true | `/news/search-update-march` |
How slugs relate to the rest of the path
A slug rarely stands alone. The folder segments before it already give context, so the slug does not need to repeat them. Compare these two:
/guides/guide-to-url-slugs/repeatsguidetwice./guides/url-slugs/lets the folder carry the category.
Folders also express hierarchy. Use them when the hierarchy is real and unlikely to change, such as /guides/ or /products/. Avoid deep nesting. Each extra folder makes URLs longer and makes moving a page harder, because the slug is tied to the path above it. A flat structure with two levels is usually enough.
Generating slugs in code
Most content systems generate a slug from the title. The generator should be deterministic, so the same title always gives the same slug. Here is a basic version:
import re
import unicodedata
def slugify(title, max_length=60):
# Decompose accented letters, then drop anything that is not ASCII
text = unicodedata.normalize("NFKD", title).encode("ascii", "ignore").decode("ascii")
text = text.lower()
# Replace every run of non-alphanumeric characters with a single hyphen
text = re.sub(r"[^a-z0-9]+", "-", text).strip("-")
return text[:max_length].rstrip("-")
print(slugify("Crème Brûlée Recipe: Easy & Quick!")) # creme-brulee-recipe-easy-quickThe steps do the following:
- Normalizing to
NFKDsplitsèintoeplus an accent mark. Encoding to ASCII withignorethen drops the accent mark. - Lowercasing handles the case rule.
- The regular expression collapses any run of spaces, punctuation, or symbols into one hyphen, so
:and&each become a single-. strip("-")removes hyphens at the start and end.- The slice enforces the length cap.
The truncation gotcha
The slice in the last line can cut a word in half. Here is what happens with a short limit:
text = "how-to-write-url-slugs-that-search-engines-and-readers-understand-easily"
print(text[:30].rstrip("-")) # how-to-write-url-slugs-that-seThe slug ends in se, which is not a word. Cut at a word boundary instead:
import re
import unicodedata
def slugify(title, max_length=60):
text = unicodedata.normalize("NFKD", title).encode("ascii", "ignore").decode("ascii")
text = re.sub(r"[^a-z0-9]+", "-", text.lower()).strip("-")
if len(text) <= max_length:
return text
cut = text[:max_length]
# If the cut landed inside a word, back up to the previous hyphen
if text[max_length] != "-":
cut = cut.rpartition("-")[0] or cut
return cut.rstrip("-")
title = "How to write URL slugs that search engines and readers understand easily"
print(slugify(title, max_length=30)) # how-to-write-url-slugs-thatThe or cut fallback handles one very long word with no hyphen. In that case there is no earlier boundary, so the function keeps the hard cut rather than returning an empty string.
Edge cases for generators
A generator can produce bad slugs even when the code is correct. Check these cases:
- Empty results. A title made only of non-Latin characters, such as
日本語, becomes an empty string after the ASCII step. Fall back to an ID or a manually written slug. Chapter material on non-English text covers better options. - Collisions. Two articles titled
Introductionproduce the same slug. Check uniqueness within the folder and append a short disambiguator, preferably a meaningful word, not a bare number. - Slug drift. If the slug is regenerated automatically every time the title is edited, a typo fix in the title changes the URL. Generate the slug once at creation, then store it. Treat it as separate data from the title.
A decision flow
The following flow summarizes how to settle on a slug for a new page:
Common mistakes
- Keyword stuffing. Repeating terms or adding every variation adds no value, and it looks spammy to readers.
- Matching the full title. Long titles make long slugs. Pick the core words instead.
- Changing slugs for cosmetic reasons. A slug that is slightly imperfect but stable is better than a perfect slug that required a redirect. Change a slug only when it is wrong or misleading.
- Using stop-word removal blindly. Automated removal of every short word can turn
how-to-use-itintouse. - Forgetting trailing-slash consistency. Choose either
/guide/or/guideacross the site and keep it, because the two forms can be treated as separate URLs.
Checklist
Before publishing, confirm each item:
- The slug is lowercase, with hyphens as the only separator.
- It has two to five meaningful words and describes the page.
- It contains no dates, version numbers, or status words that will go stale.
- It does not repeat words already in the folder name.
- It is unique in its folder.
- It is stored as its own field, not regenerated from the title.
Exercise
A CMS generates slugs from titles with a 25-character limit using text[:25].rstrip("-"). An editor publishes "Internal Linking Strategies for Large Sites". What slug results, and what two problems should you check for in this generator?
Show answer
The normalized text is "internal-linking-strategies-for-large-sites". Its first 25 characters are "internal-linking-strateg", so the slug is "internal-linking-strateg". The first problem is that the cut lands inside a word, so the slug ends with a partial word. Fix it by backing up to the last hyphen before the limit, which gives "internal-linking". The second problem is regeneration. If the generator reruns when the title is edited, the URL changes and needs a redirect, so the slug should be generated once and stored.
Exercise
Rewrite /Blog/The_Best_SEO_Tips_Of_2019_Updated as a good slug under /blog/, and explain each change.
Show answer
/blog/seo-tips. Lowercase fixes the case problem, hyphens replace underscores, the filler words "the" and "of" are removed, "best" is dropped as an unprovable claim, and "2019" and "updated" are removed because they will go stale. The folder /blog/ already supplies the category.