codeigniter 将非 www 重定向到 www
我看到 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在index.php的开头:
编辑
我看错你的问题了,答案是:
Try, in index.php, at the beginning:
EDIT
I read your question wrong, the answer is:
在 codeigniter 的 .htaccess 文件中包含这两行,以将非 www url 重定向到 www
,即完整的 .htaccess 文件将类似于 ->
include these two lines in the .htaccess file of codeigniter to redirect non-www url to www
i.e the full .htaccess file will be something like that->
使用此代码
Use this code
我知道,这个问题已经有了公认的解决方案。但如果域还有一些子域,就会出现问题。如果您的 codeigniter 也处理子域,子域也将被重定向到 www。
这是本例的答案。假设您的域名是 www.thedomain.com,代码应该是:
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:
您可以在所有 codeigniter 网站中使用以下代码。
`
`
you can use below code in all codeigniter website.
`
`