.htaccess Redirect non-WWW to WWW preserving URI string
snippet in htaccess

.htaccess Redirect non-WWW to WWW preserving URI string

user6661

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

.htaccess Redirect non-WWW to WWW preserving URI string

user1309

RewriteEngine On                           

#This bit rewrites your host name to include www
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC,L]

#This bit does the codeigniter magic
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ /index.php/$1 [L]

Redirect www to non-www

Noob codes official

RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]

RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]