gpt4 book ai didi

php - codeigniter 将非 www 重定向到 www

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:52 24 4
gpt4 key购买 nike

我看到了.htaccess Redirect non-WWW to WWW preserving URI string但它对我不起作用

如果我去 mysite.com/site/something 我会被重定向到 mysite.com/something

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

也尝试过:

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;

}

最佳答案

尝试,在 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
}

关于php - codeigniter 将非 www 重定向到 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7666675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com