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 (2)
- in the chapter, "SEO"
- tagged with dashes, htaccess, mod_rewrite, redirects, underscores
InterAction:
YourThoughts?
(Minutia)
- Author:Jesse
- Published:Sep 20, 2007
- Chapters:
- Previous:
- Next:
GetUpdated
ElseWhere
AllChapters
- 2 articles in the chapter Accessibility
- 6 articles in the chapter Announcements
- 4 articles in the chapter Blogging
- 7 articles in the chapter CMS
- 1 articles in the chapter Code
- 3 articles in the chapter CSS
- 15 articles in the chapter Design
- 4 articles in the chapter Downloads
- 1 articles in the chapter Freebies
- 1 articles in the chapter Gadgets
- 4 articles in the chapter Journeys
- 1 articles in the chapter Miscellany
- 59 articles in the chapter Movable Type
- 2 articles in the chapter Plugins
- 1 articles in the chapter Print
- 1 articles in the chapter Projects
- 2 articles in the chapter SEO
- 6 articles in the chapter Social Networking
- 1 articles in the chapter Standards
- 1 articles in the chapter Writing
BiteSize.blog
ReaderFavorites
- CSS Image Framing
- Tools of the Web Design Trade, Pt.1: Where to Begin
- Tools of the Web Design Trade, Pt.2: Building Trust
- 10 Tips For Creating Website Mockups In Photoshop
- Movable Type As A Desktop App
- Redesign, Part 1: The Logo
- Redesign, Part 2: Stylesheet Philosophy
- The Social Graph in Plain Language
- Writing Your Own Autobiography: The New Persistence of Information
- Designing eComm 2008


















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!