Using .htaccess Voodoo For Underscore and Dash Woes
Here's a little tip for people who have recently switched to dashes (-) from underscores (_) as word separators and wish all those old inbound links could still work. Try this in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ http://example.com/$1-$2-$3-$4-$5-$6.html [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ http://example.com/$1-$2-$3-$4-$5.html [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ http://example.com/$1-$2-$3-$4.html [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_(.*) \.html$ http://example.com/$1-$2-$3.html [R=301,L]
RewriteRule ^([^_]*)_(.*) \.html$ http://example.com/$1-$2.html [R=301,L]
Here's how it works: if a request comes into your server for an .html files with underscores, it redirects that request to the corresponding file with dashes. So if you've got a blog that was publishing files_like_this.html and now is publishing files-like-this.html, the old links from a friend's site to the a_file_like_this.html will be redirected to it's dashed counterpart.
The first RewriteRule deals with entries that have 5 underscores. The second deals with entries that have 4, and so on. I could have written a regular expression that would redirected each time an underscore was written, but you'd end up with your server redirecting 4 or 5 times, which would be oppressively slow and taxing on your server, neither acceptable side effects.
This technique, on the other hand, limits the redirects to one; the only drawback is that if you've got more than 5 underscores, you'll need to add another RewriteRule above the first one with extra wildcards in the regex and a -$7 at the end. A reasonable price to pay for snappier server response. Also, keep in mind that you may need to swap out extensions if need be (for instance, you'll notice that my articles have no extensions--cruft-free--so I took out the extensions altogether from the RewriteRules).
- Comments (1)
- in the chapter, "SEO"
- tagged with dashes, htaccess, mod_rewrite, redirects, underscores
InterAction:
YourThoughts?
(Minutia)
This entry was written by Jesse on Thursday, September 20, 2007 at 11:58 AM and appears in the SEO chapter. The previous article was entitled, "Redesign, Part 2: Stylesheet Philosophy", and the next entry is called, "The Social Graph in Plain Language". Bookmark the permalink, save it to del.icio.us or Digg it.
GetUpdated
ElseWhere
AllChapters
- 2 articles in the chapter Accessibility
- 2 articles in the chapter Announcements
- 3 articles in the chapter Blogging
- 5 articles in the chapter CMS
- 3 articles in the chapter CSS
- 7 articles in the chapter Design
- 3 articles in the chapter Downloads
- 1 articles in the chapter Gadgets
- 4 articles in the chapter Journeys
- 1 articles in the chapter Miscellany
- 4 articles in the chapter Movable Type
- 1 articles in the chapter Print
- 1 articles in the chapter SEO
- 4 articles in the chapter Social Networking
- 1 articles in the chapter Standards
- 1 articles in the chapter Writing


















22 October 20071. Cheryl:
Thank you! But there is an unnecessary space before \.html$ in the $1-$2 and $1-$2-$3 lines. The spaces causes a 500 error.
Since my extensions are php, I also changed html to php.
It works great!