codeigniter 将非 www 重定向到 www

发布于 2024-12-08 01:12:12 字数 1043 浏览 0 评论 0原文

我看到 .htaccess 将非 WWW 重定向到 WWW 保留 URI 字符串< 它对我不起作用,

/a> 但如果我转到 mysite.com/site/something

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

我会被重定向到 mysite.com/something也尝试过:

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

编辑:

这是我使用的代码,基于 Alfonso Rubalcava 的答案:

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;

}

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('Location: http://www.site.com/site/');
        exit;
    }

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清旖 2024-12-15 01:12:12

尝试在index.php的开头:

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
}

编辑
我看错你的问题了,答案是:

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
}

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
}
素手挽清风 2024-12-15 01:12:12

在 codeigniter 的 .htaccess 文件中包含这两行,以将非 www url 重定向到 www

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

,即完整的 .htaccess 文件将类似于 ->

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

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>
蝶舞 2024-12-15 01:12:12

使用此代码

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

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]
请远离我 2024-12-15 01:12:12

我知道,这个问题已经有了公认的解决方案。但如果域还有一些子域,就会出现问题。如果您的 codeigniter 也处理子域,子域也将被重定向到 www。

这是本例的答案。假设您的域名是 www.thedomain.com,代码应该是:

$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']);
}

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']);
}
夜未央樱花落 2024-12-15 01:12:12

您可以在所有 codeigniter 网站中使用以下代码。

`

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

`

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>

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文