A bit of a caution is in order here...
The space character is one of those special charcters in mod_rewrite. So anytime you're trying to detect on you have to first escape it with a backslash character so that it is treated literally. In your example above, you would need to change
so that it reads
As a second caution, you'll want to place any space detecting rewrite as close to the top of your .htaccess file as possible. If it won't interfere with other rewrite rules you'll want it at the very top. You definitely want it before you start tyring to come up with the final url.
I've never done this with query strings, but I have had to do it a time or two when someone was moving a site from a IIS server to a *nix server where there were file names that had a space in them, which isn't allows on a *nix server. The theory is the same, you're just working on a different part of the url string. The following is what I used in those cases, so should get you pretty close. (I've added some line spacing and comments to help point out what section is doing what.)
RewriteEngine on
# Replace spaces with hypens
RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=replace_space:yes,N]
#Redirect replace_space positives to the updated URL
RewriteCond %{ENV:replace_space} yes
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
In the above the first RewriteRule and anything that preceeds it will be processed over and over until no other spaces exist in the URL string. This is why it's important to have your space detection as close as possible to the top of your .htaccess file.
Does that help?
I'll try to find some time before the Super Bowl Party preperations begin today to see if I can't construct a little test for spaces in query strings. I don't think it should be too hard, but not having done it before one never knows. I'm not sure how much spare time I'll have to play with it today though.









