Too much turkey - replace hyphen with underscore

Topics: User Forum
Nov 23, 2007 at 1:57 AM
I am trying to rewrite the urls on our website to be a little less complicated. Its relatively simple like the example here.
/books-for-school/  ==>  /bookcatalog/books_for_school.asp
"school" is the variable because there are many other variants like books-for-kids, books-for-fun, etc. The following rule handles these single word variables fine.

RewriteRule ^/books-for-([^/]+)/$ /bookcatalog/books_for_$1.asp
However the following example comes up too:
/books-for-young-adults/  ==> /bookcatalog/books_for_young_adults.asp
The prior rule results in :
and thus throws an error.
So I put together these three runs to test for 1, 2 and 3 words variants (they must be in descending order).

RewriteRule ^/books-for-([^/]+)-([^/]+)-([^/]+)/$ /bookcatalog/books_for_$1_$2_$3.asp
RewriteRule ^/books-for-([^/]+)-([^/]+)/$ /bookcatalog/books_for_$1_$2.asp
RewriteRule ^/books-for-([^/]+)/$ /bookcatalog/books_for_$1.asp

This works but it seems like a brute force approach and I will have a lot of rules to process if I do this for every url.
I know this should be a simple replace the hyphen with an underscore exercise but I've completely drawn a blank (perhaps too much turkey).

I'd appreciate a wake up slap on this one.