There's a lot of articles about using dashes as word separators in urls - however none of them really cover the edge cases like hyphenated words or composite words with spaces in the url.

I have a real-estate site that uses hyphens as separators, and the relative path contains a lot of information about the property. And example might be: /property/qld/nundah-4012/81-nellie-st-nundah-qld-4012-4138766
It is of the format /[property-type]/[state]/[suburb]-[postcode/zip]/[street number][street name][street type][suburb][state][postcode/zip][propertyId].

The problem comes when we have suburbs or streets that are hyphenated or have spaces in them - like 'Brighton-Le-Sands' or 'Eight Mile Plains' - when we use dashes as separators the format of the word is lost.

We are using regex on the url and simply removing all dashes for spaces returns incorrect tokens when there's hypenated or space separated words. We can use pluses (+) for words with spaces, but what about hyphenated words? Does anyone out there have experience with this?

  • I'd recommend putting your property id near the beginning of your URL path. When you have long URLs, they tend to get truncated in emails or by CMS systems. It is easier to correct for truncated URLs when the id is at the beginning than when it is at the end and gets lopped off. – Stephen Ostermiller Nov 26 '18 at 16:30

Use an underscore (_) to seperate words and serve as a space.
Use a hyphen (-) in a postal code, phone number, or when otherwise appropriate.

Note: I neglected to think on the SEO side of things... red_sneakers is the same as redsneakers to Google

  • Underscores in URLs is bad advice because Google doesn't tread underscores as word separators as per your link. – Stephen Ostermiller Nov 26 '18 at 16:15
  • Yes, that is why I appended my answer with the link. I didn't realize it at the time. That doesn't mean you cannot use underscores, only that Google (and likely other) search engines don't recognize the underscore character. Perhaps that will change in the future. – elbrant Nov 26 '18 at 16:20

The usual practice for SEO slugs is:

  • Use all lower case
  • Replace one or more non-letter characters with a hyphen
  • Remove hyphens from the beginning and end

In pseudo code that would be:

slug = lowercase(title)     # First lower case the title
slug =~ s/[^a-z]+/-/g       # Then replace non-letter occurances with hyphen
slug =~ s/(^-)|(-$)//g      # Then remove hyphens from the beginning and end

That means that both hyphens and spaces, as well as underscores and other punctuation are represented by hyphens in the URL slug. That ends up being fairly readable while preserving SEO value of words in the URL.

So your examples would end up being:

  • /brighton-le-sands
  • /eight-mile-plains

You may need to have other rules. For example you may want to deal with possessives (like Jim's) differently. I prefer jims in the URL slug rather than jim-s. If you have international characters in your words, you may want to replace accents with an ASCII equivalent, or allow more than just the 26 letters of the alphabet in your slugs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.