我看到.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
}