Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess Redirect non-WWW to WWW preserving URI string

I'm running the CodeIgniter platform, which uses .htaccess to accept URLs like

http://www.mysite.com/controller/function/argument

I currently use some .htaccess rewrites, namely (simplified):

RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/index\.php|/images|/assets)
RewriteRule ^(.*)$ /index.php/$1 [L]

I want to add a rewrite rule that redirects all non www requests to www. I also want the URI string following the domain name to stay the same in the redirect. For instance, if a user makes a request for http://mysite.com/controller/function/argument, I want the .htaccess file to rewrite the request in the browser as http://www.mysite.com/controller/function/argument and then process the request.

like image 429
Steven Avatar asked Nov 06 '09 07:11

Steven


1 Answers

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

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]
like image 195
ekhaled Avatar answered Sep 20 '22 23:09

ekhaled