FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Rewrite underscores to hyphens for SEO URL

by 11 comments

This Apache .htaccess code 301 redirects requests for URLs with underscores (_) to the URL with hyphens.

Please see this article for the working updated code.


SEO benefits of using hyphens/dashes over underscores

According to Matt Cutts the choice is clearly to use dashes or hyphens instead of underscores.

So instead of article_this_is_awesome_page.html you want article-this-is-awesome-page.html.

I often get asked whether I'd recommend dashes or underscores for words in urls. For urls in Google, I would recommend using dashes.


Updated Htaccess

Thanks to lucus for supplying this improved version:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
 
RewriteCond %{REQUEST_URI} !\.(html|php)($|?) [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
 
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L]

Example Usage

When http://d.com/the_top_apache_htaccess_article.html is requested by a browser or search engine, a Search-Engine Friendly 301 Redirect is sent telling the client that the correct location for the resource is at http://d.com/the-top-apache-htaccess-article.html

The Apache .htaccess underscore to hyphen conversion code

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
 
RewriteRule !\.(html|php)$ - [S=6]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]
 
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://www.askapache.com/$1 [R=301,L]

Further Reading

  1. The Worlds Greatest Apache .htaccess Guide
  2. Article in htaccessElite forum:Rewrite urls separated by _ underscores to - dashes

Tags

February 22nd, 2007

Comments Welcome

  • Joe

    Hi - Great resource. At the risk of sounding ignorant, could you please tell me where abouts (i.e. in which directory/folder/subdirectory I would place this code to ensure the rewrite to dashes works? Thanks

  • http://neocall-tr.com/ Faruk

    Hello can anyone tell me how can i edit a htaccess multicommand file?i m currently using user friendly code and i added codes for broken links and code for fclearing query_strings
    but at the and it did not work:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /friendly_url.php

    ErrorDocument 400 http://www.sicaktakip/error_404.html
    ErrorDocument 401 http://www.sicaktakip/error_404.html
    ErrorDocument 403 http://www.sicaktakip/error_404.html
    ErrorDocument 404 http://www.sicaktakip/error_404.html
    ErrorDocument 500/http://www.sicaktakip/index.php
    RewriteCond %{QUERY_STRING} ^source= RewriteRule (.*) /$1? [R=301,L]
    Anyone any idea for that?

  • http://www.aquaculturecouncilwa.com/about_acwa/acwa_committee_of_management mark

    Haven't be able to get this to work, and I've tried all of the variations in the two further reading links.

    I have the file extensions removed on the old site and on the new, so all I really need to do is rewrite the underscores to dashes.

    Love to know where I'm going wrong but not being a tech, I don't really know where toe start.

    PS At the moment I'm testing on a local wamp setup before I go messing around with a live environment.

  • lucas

    it would be better to have html and php extensions skipped using a condition yet still allow querystrings. but more importantly to use one rule to replace underscores and loop it with the N flag instead of trying to anticipate how many there'll be. this version addresses those issues and trims consecutive underscores to a single hyphen.

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} !.(html|php)($|?) [NC]
    RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
    RewriteCond %{ENV:underscores} ^Yes$
    RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L]

  • stefan

    Hello i just wondering if there is anyway to rewrite underscores to slashes.

    Thanks in advance, sorry for my bad english.

  • Jake

    how can you use this rewrite but exclude directories

  • http://crnano.org Chris Phoenix

    The article's example code has S=5 option (skip next five rules) followed by six underscore-elimination rules.

  • Andriy

    And what's your suggestion if the URL can include up to 16 underscores that should be rewritten to hyphens?

  • saurabh

    How i redirect url to error page when we find & or = in url
    Please give me the solution

  • Daniel Harris

    I get a server error 500 when I try this code.

  • prabhu

    how to remove file extension here?

Popular Articles
My Online Tools

Related Articles
Newest Posts
Twitter


  •  t.co/ShKrGdqXuJ 
  • RUN GCC! This is a typical shirt I wear, from the  t.co/46LYbFr4k2  shop. A clerk at the LQ recognized it!  t.co/jjmT0dkCPu 
  • Merlin the Magician  t.co/iMmRbanUi4 
  • ROGUE CODE - Latest novel from @markrussinovich  t.co/apkn0LoPIt 
  • RTFM - surprisingly very helpful and way more comprehensive than it looks! @redteamfieldman #pwnAllTheThings  t.co/xiaJ5g0aC9 
  • Dear Hacker - Letters to the Editor of 2600, from Emmanuel Goldstein  t.co/JCfLab7FAJ 
  • The Mythical Man-Month - Essays on Software Engineering, by Frederick P. Brooks, Jr.  t.co/ilWN5GHElr 
  • "where wizards stay up late" - The Origins of the Internet. Favorite book detailing the birth of the net and IMPs  t.co/gY9VTGJgZz 
  • ZERO DAY - read before Trojan horse  t.co/pPMLGDJv8P 
  • Trojan Horse, a novel!  t.co/Hf8EtYaZVa 
  • The Hacker Playbook - very nice high level overview of attacks  t.co/lHwNVWi61u 
  • Clean Code - A Handbook of Agile Software Craftsmanship  t.co/hnJX0x1qIc 
  • Secrets of the JavaScript Ninja - By my absolute favorite JS hacker John Resig!  t.co/tZ42ljmcCl 
  • Hacking Exposed 7: Network Security Secrets & SolutionsMy all time favorite, basic but thorough and accurate.  t.co/jycW0RDVtZ 

Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman






[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

| Google+ | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain