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!


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

Find me on aim Find me on delicious Find me on digg Find me on linkedin Find me on flickr 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

ActionStream

  • Jesse said, "Picking up the crib, taking the relatives to Ikea and then off to Penn's Landing for festivities." Jesse said, “Picking up the crib, taking the relatives to Ikea and then off to Penn’s Landing for festivities.” 2008-07-05T15:11:50Z 2008-07-05T15:11:50Z
  • Jesse said, "I just figured out why I love movies. They relax my brain. Was lying there watching and getting sleepy. Went to bed, now wide awake." Jesse said, “I just figured out why I love movies. They relax my brain. Was lying there watching and getting sleepy. Went to bed, now wide awake.” 2008-07-05T05:11:10Z 2008-07-05T05:11:10Z
  • Jesse said, "Finishing the evening off right by watching Saving Private Ryan." Jesse said, “Finishing the evening off right by watching Saving Private Ryan.” 2008-07-05T02:50:36Z 2008-07-05T02:50:36Z
  • Jesse said, "@textism » If you're going to make sensational indictments about one recently dead, have some respect and back them up." Jesse said, “@textism » If you’re going to make sensational indictments about one recently dead, have some respect and back them up.” 2008-07-05T02:43:49Z 2008-07-05T02:43:49Z
  • Jesse said, "July 4th — all the ambience of war, none of the immediate danger." Jesse said, “July 4th — all the ambience of war, none of the immediate danger.” 2008-07-05T01:34:11Z 2008-07-05T01:34:11Z
  • Jesse said, "@snookca » Somebody asked me that once and I told them $250k." Jesse said, “@snookca » Somebody asked me that once and I told them $250k.” 2008-07-04T18:01:37Z 2008-07-04T18:01:37Z
  • Jesse said, "About to wring the neck of an indecisive client from Europe; unless, of course, they provide me with delicious artisan cheeses." Jesse said, “About to wring the neck of an indecisive client from Europe; unless, of course, they provide me with delicious artisan cheeses.” 2008-07-04T14:28:48Z 2008-07-04T14:28:48Z
  • Jesse said, "INBOX: 0 — HAPPY INDEPENDENCE DAY!!!" Jesse said, “INBOX: 0 — HAPPY INDEPENDENCE DAY!!!” 2008-07-04T07:16:07Z 2008-07-04T07:16:07Z
  • Jesse said, "@tdhedengren » Mark all, archive. Makes me somehow feel like a better person." Jesse said, “@tdhedengren » Mark all, archive. Makes me somehow feel like a better person.” 2008-07-04T06:34:09Z 2008-07-04T06:34:09Z
  • Jesse said, "54 messages left in my inbox. Can I make it?" Jesse said, “54 messages left in my inbox. Can I make it?” 2008-07-04T06:22:25Z 2008-07-04T06:22:25Z