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).

  • posted on 20 September 2007
  • by Jesse

InterAction:

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!

1 March 20092. val ter:

Great, found the answer here.

p.s i like the design of this site


YourThoughts?



(Minutia)

  • Author:
    Jesse
  • Published:
    Sep 20, 2007
  • Chapters:

GetUpdated

ElseWhere

Find me on aim Find me on delicious Find me on digg Find me on linkedin Find me on pownce Find me on twitter Find me on youtube Find me on skype Find me on facebook Find me on livejournal Find me on msn Find me on vox Find me on technorati Find me on yahoo Find me on dopplr Find me on lastfm

ActionStream

  • Jesse saved The Declaration of Independence as a favorite video Jesse saved The Declaration of Independence as a favorite video 2009-07-04T21:00:08Z 2009-07-04T21:00:08Z
  • Jesse said, "We hold these truths to be self-evident. http://tr.im/qT9K (via @nathansmith)" Jesse said, “We hold these truths to be self-evident. http://tr.im/qT9K (via @nathansmith)” 2009-07-04T20:36:56Z 2009-07-04T20:36:56Z
  • Jesse said, "It's funny how difficult Drudge's headlines are to read in Independence Day blue." Jesse said, “It’s funny how difficult Drudge’s headlines are to read in Independence Day blue.” 2009-07-04T17:09:35Z 2009-07-04T17:09:35Z
  • Jesse said, "@foolmother Hilton did the same thing to me as well." Jesse said, “@foolmother Hilton did the same thing to me as well.” 2009-07-04T13:40:20Z 2009-07-04T13:40:20Z
  • Jesse said, "The Adventures Of An Independent Contractor: Client: "We won't be in tomorrow." Me: "Why not?!" Client: "Uh—the holiday." Me: "Oh—riiight."" Jesse said, “The Adventures Of An Independent Contractor: Client: "We won’t be in tomorrow." Me: "Why not?!" Client: "Uh—the holiday." Me: "Oh—riiight."” 2009-07-04T13:22:02Z 2009-07-04T13:22:02Z