.htaccess underscores
September 24, 2004 12:15 PM   Subscribe

.htaccess question. I want to turn all the underscores in a url into dashes. More inside..

I'm starting to use the new dynamic page option MT 3.1 offers, and using the change as an opportunity to redesign and 'clean house'. I've read about how using underscores for dirification doesn't work as well with search engines as using dashes. I've got my pages in my fresh build using dashes, but I want to redirect all of the inbound links, search engine requests, etc. to the new version.

But I don't know how. Currently, my archive structure looks like this:

/tester/archives/2004/09/10/mt_31_installation

But, I want it to look like this:

/tester/archives/2004/09/10/mt-31-installation

Any ideas about the code involved? The structure is pretty rigid:

/YYYY/MM/DD/dirified-title-with-multiple-words
posted by jmevius to Computers & Internet (6 answers total)
 
Try this...

RewriteEngine On
RewriteRule ^([^_]*)_(.*)$ $1-$2 [R=301,L]
posted by Civil_Disobedient at 1:33 PM on September 24, 2004


You'll have to extend it for more than one dash, however.

rewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ $1-$2-$3-$4-$5.html [E=unscors:Yes]
rewriteRule ^([^_]*)_([^_]*)_(.*)\.html$ $1-$2-$3.html$ [E=unscors:Yes]
rewriteRule ^([^_]*)_(.*)\.html$ $1-$2.html [E=unscors:Yes]
#
rewriteCond %{ENV:unscors} ^Yes$
rewriteRule ^(.*)\.html$ http://www.example.com/$1.html [R=301,L]
posted by Civil_Disobedient at 1:38 PM on September 24, 2004


Argh, one more time... (make sure your browser is at full width so you see where the line breaks are.)

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ http://www.mysiteurl.com/$1-$2-$3-$4-$5.html [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ http://www.mysiteurl.com/$1-$2-$3-$4.html [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_(.*)\.html$ http://www.mysiteurl.com/$1-$2-$3.html [R=301,L]
RewriteRule ^([^_]*)_(.*)\.html$ http://www.mysiteurl.com/$1-$2.html [R=301,L]

posted by Civil_Disobedient at 1:41 PM on September 24, 2004


Response by poster: Thanks. That did it.
posted by jmevius at 1:55 PM on September 24, 2004


is this mod_rewrite magic?
posted by scarabic at 9:15 PM on September 24, 2004


It is indeed. Regular expressions are your fiend.
posted by Civil_Disobedient at 1:01 AM on September 25, 2004


« Older Which of Aristotle's works is this quote from?   |   Movable Type Newer »
This thread is closed to new comments.