๐Ÿ‹
Menu
Best Practice Beginner 1 min read 243 words

Slug Generation and URL-Safe String Best Practices

Generate clean, SEO-friendly URL slugs from titles and names, handling Unicode, transliteration, and edge cases.

URL Slug Generation

Clean URL slugs improve SEO, readability, and shareability. Converting arbitrary text into URL-safe strings requires handling Unicode characters, multiple whitespace patterns, and special characters consistently.

Basic Slug Rules

A well-formed slug uses only lowercase ASCII letters, digits, and hyphens. Leading and trailing hyphens are removed. Multiple consecutive hyphens are collapsed to one. Common word separators (spaces, underscores, dots) become hyphens. The result should be a readable, meaningful summary of the content.

Unicode and Transliteration

For international content, transliterate Unicode characters to ASCII equivalents before slugifying: รผโ†’ue, รฑโ†’n, ๆ—ฅๆœฌโ†’nihon. Libraries like python-slugify and slugify.js handle this automatically for most scripts. For CJK characters without standard transliterations, consider using romanization systems (Pinyin, Romaji) or keeping the original characters in the URL if your stack supports IRI.

SEO Considerations

Keep slugs under 60 characters โ€” search engines may truncate longer URLs. Remove stop words (the, a, an, is, of) to focus on keywords, but only if the result remains readable. Avoid including dates or version numbers unless the content is inherently temporal. Once a slug is published and indexed, never change it without setting up a 301 redirect.

Collision Handling

When two different titles generate the same slug, append a numeric suffix (my-title, my-title-2, my-title-3). Check for collisions at generation time, not just at database insert time, to provide immediate feedback. For high-volume systems, consider appending a short random suffix instead of sequential numbers to avoid race conditions.

้–ข้€ฃใƒ„ใƒผใƒซ

้–ข้€ฃใƒ•ใ‚ฉใƒผใƒžใƒƒใƒˆ

้–ข้€ฃใ‚ฌใ‚คใƒ‰