This Apache .htaccess code 301 redirects requests for URLs with underscores (_) to the URL with hyphens.
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.
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=5]
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://d.com/$1 [R=301,L]
Further Reading
- The Worlds Greatest Apache .htaccess Guide
- Article in htaccessElite forum:Rewrite urls separated by _ underscores to - dashes
requests convert transform replace rewriterule rewritecond rewriteengine rewritebase followsymlinks
Related Articles
- htaccess HTTPS / SSL Tips, Tricks, and Hacks
- Replacing ‘%23′ with ‘#’ in incoming links
- htaccess rewrite, Mod_rewrite tricks
- Smart HTTP and HTTPS RewriteRule Redirects
- Make phpBB SEO friendly with htaccess
- 301 Redirect with mod_rewrite or RedirectMatch
- Hacking WP Super Cache for Speed
- .htaccess mod_rewrite rewrite examples

02.13.08 at 5:02 pm
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
04.23.08 at 8:33 pm
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?