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.

  • 写回答

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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 模拟电路求复阻抗和传递函数
  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio