开发者

codeigniter redirect non-www to www

开发者 https://www.devze.com 2023-04-11 14:17 出处:网络
i saw .htaccess Redirect non-WWW to WWW preserving URI string but it doesn\'t work for me if i go to mysite.com/site/something i get redirected to mysite.com/something

i saw .htaccess Redirect non-WWW to WWW preserving URI string but it doesn't work for me

if i go to mysite.com/site/something i get redirected 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]

edit:

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

if (substr($_SERVER['SERVER_NAME'], 0, 3) != 'www')
{

    if ($_SERVER['REQUEST_URI'] == '//site/')
    {
        header('HTTP/1.1 301 Moved Permanently');
        header('Loca开发者_如何转开发tion: http://www.site.com/site/');
        exit;
    }

    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://www.site.com' . $_SERVER['REQUEST_URI']);
    exit;

}


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 read your question wrong, 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
}


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

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

i.e the full .htaccess file will be something like that->

<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>


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]


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

Here is the answer in this case. Let's say that 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']);
}


you can use below code in all codeigniter website.

`

<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

精彩评论

暂无评论...
验证码 换一张
取 消