Text & writing
Hyphens vs. Underscores in URLs
Should a web address use learn-piano-chords or learn_piano_chords? The short answer for public web pages is hyphens. This guide explains why, and the situations where underscores are still the right choice.
Ready to try the tool this guide describes?
Why hyphens are the convention
Google’s own guidance on URL structure recommends hyphens to separate words in addresses, because hyphens are treated as word separators while underscores are not reliably treated that way. Hyphens are also easier to see in links, which are often shown underlined — an underline can hide an underscore entirely.
Side by side
| Hyphen (my-page) | Underscore (my_page) | |
|---|---|---|
| Read as separate words by search engines | Yes | Not reliably |
| Visible in an underlined link | Yes | Often hidden |
| Common for public web pages | Yes | Rare |
| Common for code identifiers and file names | Sometimes | Yes |
When underscores make sense
- Variable names, database fields, and identifiers in programming languages where hyphens are not allowed.
- File names in systems or teams that already use underscores consistently.
- Existing URLs that already use underscores and are well linked — changing them means redirecting every address.
Consistency matters most
Whichever separator you pick for a site, use it everywhere. Mixing styles makes addresses harder to guess and type, and can lead to near-duplicate pages. Spaces should never appear in a slug; they are encoded as %20 and look broken when shared.
Hyphens vs. Underscores FAQ
- Are hyphens or underscores better in URLs?
- Hyphens, for public web pages. Search engines treat them as word separators and they stay visible in underlined links.
- Do underscores hurt search rankings?
- They are not a penalty, but words joined with underscores may not be recognised as separate words, which is why hyphens are recommended.
- Should I change existing URLs from underscores to hyphens?
- Usually not worth it for established pages. If you do, set up permanent redirects from every old address.
- When should I use underscores?
- For code identifiers, database fields, and file-naming systems that already use them.
- Can a URL contain spaces?
- Not directly. Spaces are encoded as %20, which looks messy, so slugs use hyphens instead.
Related guides
- What Makes a Good URL SlugHow to write short, readable, lowercase URL slugs, what to leave out, and why changing a slug after publishing needs a redirect.
- Accents and Symbols in SlugsHow slug generators handle accented letters, apostrophes, ampersands, emoji, and non-Latin alphabets, and how to choose a consistent policy.
Open the tool
Jump into URL Slug Generator when you are ready to process your files.
