.htaccess Redirect non-WWW to WWW preserving URI string .htaccess Redirect non-WWW to WWW preserving URI string apache apache

.htaccess Redirect non-WWW to WWW preserving URI string


I had a similar problem, and this .htaccess works for me

RewriteEngine On                           #This bit rewrites your host name to include wwwRewriteCond %{HTTP_HOST} !^www\.example\.com [NC]RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC,L]#This bit does the codeigniter magicRewriteCond %{REQUEST_FILENAME} !-f  RewriteCond %{REQUEST_FILENAME} !-d  RewriteRule ^(.*)$ /index.php/$1 [L]


It should be something like this:

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


If you are on apache 2.4 ,You can use the following simple redirect :

<if "%{HTTP_HOST} =='example.com'">Redirect / http://www.example.com/</if>