RewriteRule /tag/^(.*)\.htm$ /index.php?route=home/find&keyword=$1 [NC]
What is the sequence
to mean? As written, in non-initial position it is meaningless and possibly illegal. (I am not prepared to try it in Apache. In the text editor it didn't spit up an error message, it just didn't do anything.) There is a recent post by g1smd addressing this very point, but considering how hard it was to find my own
post about spaces, I'm not even going to try looking for it :)
It's not working, because I'm not sure how to replace the space in $1 with a hyphen... any ideas?
Replacing one space is trivial. Replacing an indefinite number is tricky. My first thought was
RewriteRule \ - [N]
That's: one literal space character, escaped, followed by an un-escaped space for transition from input to output. But you can't do that, because the hyphen alone has special meaning in the replacement string. You'd have to escape it to \- to output a literal hyphen-- and you're not supposed to escape in the replacement string.
Better try it this way:
RewriteRule ([a-z])\ ([a-z]) $1-$2 [N]
For maximum protection I've enclosed the space in letters on both sides. (Add \d if necessary.) USE [N] WITH EXTREME CAUTION. It means "go back to the beginning as many times as necessary".
:: detour here to find near-identical post from within the last two or three days ::
(###! Can't figure out how to link directly to post #4335955 near the bottom of the thread, dated July 6.)
As noted over there, if you need to do something with spaces, it is safest to make a separate rule for each possible number of spaces, assuming you'll never have more than nine (!) of them. For example
RewriteRule (my)(?:%20|\ )(search)(?:%20|\ )(term) $1-$2-$3 [NC]
Since your replace concerns the query, there will be some additional jiggery-pokery, but that's the basic principle. Note the (?: ) so you don't capture the spaces and get them mixed up with the parts you do
want to keep. If the spaces have been genuinely obliterated in transit, rather than just encoded to %20, there's not much you can do unless there is a relatively small number of affected query strings that you can fix manually.
Once in a blue moon you'll meet something that somehow got double-encoded, like %2520 which reduces to %20. But this is all painful enough already.