I don’t know if I’ve understood your question properly. Are you trying to redirect from domain.com to www.domain.com, so that the www is always used? If so, this should work:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]
A Redirect like that would cause a loop IF located in the domain.com’s DocumentRoot. TB’s code is accurate, correct and the most efficient use of mod_rewrite. Your “it doesn’t work” is a useless comment, though, as you did not give any indication of what’s not working (if mod_rewrite is not enabled, you should get 500 errors; if it is, .htaccess should be the file examined by Apache - you are using Apache, aren’t you? - and the code is picture perfect).
As usual (with mod_rewrite questions), please specify EXACTLY what you’re trying to do and provide us with the code you’re using as the code often has some error which can be quickly spotted by the trained eye.
Sometimes your server can be picky on what it will do inside the .htaccess file. I would hit up your web hosting company about making changes, because this should work.
To say the code is not working is perfectly valid since it is not. I never said the code itself is written wrong as it is not because it is the first type of code I had tried first as it is in my coding toolbox. Though it creates issues. The server should be apache as that is what the host has told me.
We’re just not connecting. Use the following code in the DocumentRoot’s .htaccess:
RewriteEngine on
# force www but substitute your domain name
RewriteCond %{HTTP_HOST} !^www\\.example\\.com$ [NC]
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]
# place WP's code here but [COLOR="#FF0000"]WITHOUT [/COLOR]the <IfModule> wrapper, without the RewriteCond %{REQUEST_URI} index.php but with the RewriteBase /blog
RewriteBase /blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php [L]
# force NON-www but substitute your domain name
RewriteCond %{HTTP_HOST} !^example\\.com$ [NC]
RewriteRule .? http://example.com%{REQUEST_URI} [R=301,L]
Note: This will send everything (parked domains, in particular) to http://example.com.
That, too, is in the tutorial (and sample code) linked in my signature so it was there for the taking if you’d only look around just a bit.