dongzhoutuo0883
2018-09-30 23:08
浏览 42
已采纳

Codeigniter使用config.php将非www重定向到www

I know how to redirect non-www to www using the .htaccess. But for some reason, I can't do it from the .htaccess file! I would like to do this from application/config.php. There already have HTTP to https redirect. here is the code,

$root = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;

Now, Is it possible to redirect non-www to www from here? If it's possible then how?

Thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我知道如何使用.htaccess将非www重定向到www。 但由于某种原因,我不能从.htaccess文件中做到这一点! 我想从application / config.php这样做。 已有HTTP到https重定向。 这是代码,

  $ root =(isset($ _ SERVER ['HTTPS'])?“https://”:“http://”)。  $ _SERVER ['HTTP_HOST']; 
 $ root。= str_replace(basename($ _ SERVER ['SCRIPT_NAME']),'',$ _SERVER ['SCRIPT_NAME']); 
 $ config ['base_url'] = $  root; 
   
 
 

现在,是否可以从这里将非www重定向到www? 如果有可能那么?

提前致谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqwh1119 2018-10-01 02:41
    已采纳

    Ok, after trying in several ways I found a simple solution! Unfortunately, I couldn't do this from the config.php. But I did this from the index.php without using .htaccess. And for me, it's working well.

    Here is the code, that I implemented at root-> index.php

    if (substr($_SERVER['SERVER_NAME'],0,3)!="www"){
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: https://www.".$_SERVER['HTTP_HOST'] ."".$_SERVER['REQUEST_URI']);
        exit(3); // EXIT_CONFIG
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题