Sub topic 4
Writing good slugs
This chapter assumes you know the parts of a URL, including what the slug is, and that you can read basic Python and HTML. Here you will learn the rules that make a slug useful, and how to generate slugs consistently in code.
What makes a slug good
A slug is read by people and by search engines. Both want a short phrase that tells them what the page is about before the page loads. These guidelines cover most cases:
- Describe the page. Use the words a visitor would use to name the topic.
- Keep it short. Three to five words is typical. Drop filler such as "a", "the", "and", and "of" when the meaning survives.
- Use lowercase. Many servers treat
/Guideand/guideas different URLs, which creates duplicates. - Separate words with hyphens. Search engines treat a hyphen as a word separator. Underscores join words, so
url_slugscan read as one token. - Avoid dates and numbers that expire. A slug like
seo-tips-2019looks stale in 2026, and changing it later needs a redirect. - Leave out parameters and session IDs. Readable paths are easier to share and to keep stable.
| Weak slug | Problem | Better slug |
|---|---|---|
| `/Blog/Post_1234` | Mixed case, underscore, no meaning | `/blog/internal-linking-tips` |
| `/how-to-write-the-best-url-slugs-for-seo-in-2019` | Too long, tied to a year | `/blog/url-slugs` |
| `/p?id=88&ref=nav` | Parameters, no description | `/guides/url-slugs/` |
Don't repeat the folder name
If the path already says /guides/, the slug does not need to say "guide" again. Write /guides/url-slugs/, not /guides/guide-to-url-slugs-guide/. Keep keywords where they help a reader, not where they only repeat.
Generating slugs in code
If a CMS or script creates slugs from titles, apply the same rules every time. This function lowercases, replaces each run of non-alphanumeric characters with one hyphen, and caps the length.
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-quick
print(slugify("Writing good slugs for search", max_length=15)) # writing-good-slThe second call shows a common error. The length cap cuts in the middle of a word, so slugs becomes sl. When you set a cap, either cut at the last hyphen or review long titles by hand. Also remember that the slug is usually generated once, when the page is created. If you later edit the title, the slug should not change automatically, because changing a published URL breaks links and rankings unless you add a redirect. That topic has its own chapter.
How the pieces fit
Checking a slug before you publish
Ask four questions. Could someone guess the page topic from the slug alone? Is it lowercase with hyphens only? Is it free of dates and parameters? Would you be comfortable keeping it for years? If the answer to any is no, fix it now, because after publication every change costs a redirect.
Exercise
A title is "10 Best Hiking Boots of 2019 (Updated!)". Write a slug for /gear/ that follows this chapter's guidelines, and give two reasons for your choices.
Show answer
A good answer is /gear/hiking-boots. It is short and lowercase, and it uses hyphens. It drops the year, the number, and "updated", which would make the URL look outdated or force a change later. It also avoids parentheses and punctuation, so the slug stays clean.
Exercise
Why is /guides/url_slugs/ weaker than /guides/url-slugs/?
Show answer
Search engines treat hyphens as word separators but may read underscores as joining words, so url_slugs can look like a single token. The hyphen version makes each word clearer.