让PHPCMS 9.6.3支持https的方法

{{ time }}

方法说明:

1.只是让网站前台在https运行, 而后台仍在http运行

2.权定前台页面都在 m=content 下, 只是让m=content下的页面在https下访问

3.可能不适用于用js拼接了本站域名的一些情况

4.只适合展示站的情况, 不适合有表单提交数据的情况

Step1 网站域名以www.domain.com为例, 首先要配置好https,达成这种状态:

用https://www.domain.com 和 http://www.domain.com都可以访问网站, 具体方法略过

下面的配置是为了让网站在浏览器显示为"安全"; 因为当https的页面中, 调用了http的资源, 那么它在浏览器不会显示为安全.

Step2 在phpcms的根目录建立文件router.php, 存入以下代码

<?php
//设定1: http网址
$http = 'http://www.domain.com';//改成你的域名

//设定2: https网址
$https = 'https://www.domain.com';//改成你的域名

//从$http得到路由网址
$router = "$http/index.php?inrouter=1";

//读取所有参数
$params = $_SERVER['QUERY_STRING'];

//https下的后台跳至http下的后台
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' && $params == 'm=admin') {
    header("Location: $http/index.php?m=admin");
    die;
}

/** 参数解读 开始 
 *    这些条件是为判断是否为前台页面
 */

//条件1: 是前台页面, 即m=contents的
$c1 = strstr($params, 'm=content');

//条件2: 不在路由器中
$c2 = !strstr($params, 'inrouter=1');

//条件3: 是首页
$c3 = strlen($params) == 0;

//条件综合
$condition = ($c1 && $c2) || $c3;

/**参数解读 结束*/

if ($condition) {
    $uri = "$router&$params";

    $html = '';
    $html .= '<meta charset="utf-8">';//防止乱码

    //读取内容, 并把本域名下的 http:// 改为 https://
    $html .= file_get_contents($uri);
    $html = str_replace($http, $https, $html);
    /**
     * 本文的设定, 并未包含网站引用了其他域名下的资源的情况
     * 要知道https网址下, 有很多http内容不能显示, 比如css
     * 要把引用内容的网址由http改为https, 可以在此处添加代码
     */

    //显示内容
    echo $html;
    die;
}

Step3 备份PHPCMS根目录下index.php, 并编辑它, 可以把它的前两行变成这样

<?php
include 'router.php';//加载路由文件

现在就可以了