Should I Use Hyphens or an Underscore for a URL?
- Internet domain host names can contain letters, numbers and hyphens. The underscore character is not allowed in the main part of the domain host name. For example, www.sample-domain.com is a valid host name while www.sample_domain.com is not. The underscore characters are legal in the subdomain portion of the URL (sub_domain.example.com) and can be used in a local network but may not always resolve correctly when accessing it via Internet.
- Hyphens and underscore are legal characters for folder names. For example, both http://www.example.com/folder-name and http://www.example.com/folder_name are valid URLs. You can use both hyphens and underscores in a page name and are often used to indicate a space in a longer page name. For example, a page entitled “Different breeds of dogs” may have a page name of different-breeds-of-dogs.html or different_breeds_of_dogs.html.
- Choosing a naming convention to use throughout your site can help you develop a consistent pattern on where to use these special characters. This keeps the URLs consistent throughout the site and is aesthetically pleasing for your page visitors. For example, you may choose to use hyphens in the folder name while using underscores for your page names such as: http://www.sample-domain.com/dog-breeds/australian_shepherd.html
- Using hyphens and underscores can increase the length of your URL and may appear cumbersome to some of your website's visitors. If you have a long URL with hyphens or underscores in it, consider what words you can cut from the page or folder name without losing the descriptive nature. For example, you may be able to shorten http://www.sample.com/dog-breeds-of-the-world/continents _europe_and_asia into something like http://www.sample.com/world-dog-breeds/europe_and_asia