How to let non-www redirect to www domain?

Today I installed DEDECMS, a very popular PHP CMS open source system, I found I can not let non-www redirect to www domain. Say my domain is, then I can not let redirects to I tried to google, and found the following code can resovle the problems:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

Save the above code in a .htaccess file, and upload this .htaccess to your web hosting root. Everything is OK!