andreas-kalt,
Hmmm, that's the way that I created my response the first time then talked myself out of it
.
Without the conversion from - to _, the code would be
Code:
RewriteEngine On
RewriteRule ^section/([a-z0-9/]+)\.htm$ section/$1 [L]
Adding hyphens and converting to underscores to the above code is a little trickier:
Code:
RewriteEngine On
RewriteRule ^section/([a-z0-9_/]*)-([a-z0-9_/]*)\.htm$ section/$1_$2.htm [N]
RewriteRule ^section/([a-z0-9_/]+)\.htm$ section/$1 [L]
The new rule will look for a hyphen amid lowercase letters, digits, underscores and slashES and replace them ONE AT A TIME with underscores then restart and look for another hyphen.
Note 1: If you were sure that only one hyphen was in your URLs, you could drop the Next flag ([N]) (and the _'s in the first rule) and Apache would only have to make a single pass through this code.
Note 2: Regular expressions (preg_replace) are far more powerful as they'll change every occurance of a string in another string as directed but I believe that more care is required with Apache's implimentation. My use of the Next flag may be overkill on my part but it will force Apache to restart the rewrite rules with the updated {REQUEST_URI} and that's what we need to be sure we've changed all -'s to _'s.
By the time Apache gets to the second (and Last [L]) rule, all hyphens would have been converted to underscores and that's been added to that rule so you should be set.
The important thing is that you can understand what's going on with the regex used so you can do it the next time.
Regards,
DK
Bookmarks