Codeigniter redirects non-www to www

I saw .htaccess Do Not Redirect WWW to WWW, keeping URI string , but for me it does not work

if I go to mysite.com/site/something , I will redirect to mysite.com/something

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

also tried:

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

change

here's the im code using Alfonso-based Rubalcava based answer:

 if (substr($_SERVER['SERVER_NAME'], 0, 3) != 'www') { if ($_SERVER['REQUEST_URI'] == '//site/') { header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.site.com/site/'); exit; } header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.site.com' . $_SERVER['REQUEST_URI']); exit; } 
+4
source share
5 answers

Try in index.php at the beginning:

 if(substr($_SERVER['SERVER_NAME'],0,3)=="www"){ header("HTTP/1.1 301 Moved Permanently"); header("Location: http://yourdomain.tdl/".$_SERVER['REQUEST_URI']); }else{ //the current contents of your file } 

EDIT I am reading your question incorrectly, the answer is:

 if(substr($_SERVER['SERVER_NAME'],0,3)!="www"){ header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.yourdomain.tdl/".$_SERVER['REQUEST_URI']); }else{ //the current contents of your file } 
+6
source

include these two lines in the .htaccess codeigniter file to redirect non-www-url to www

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

ie the complete .htaccess file will be something like this →

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.(.*) RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> 
+2
source

Use this code

 #Redirect to www RewriteEngine On RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
0
source

I know the problem already has a solution. But there is a problem if the domain also has several subdomains. If your codeigniter also handles subdomains, the subdomains will also be redirected to www.

Here is the answer in this case. Let's say your domain is www.thedomain.com, the code should be:

 $sna = explode(".", $_SERVER['SERVER_NAME']); if($sna[0]=="thedomain"){ header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.thedomain.com/".$_SERVER['REQUEST_URI']); } 
0
source

You can use the code below on all codeigniter sites.

''

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.(.*) RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> 

''

0
source

All Articles