基于cos-html-cache实现WordPress页面纯静态化至KVDB_html/css_WEB-ITnose

php中文网
发布: 2016-06-21 08:52:25
原创
1884人浏览过

jackie在sae上搭建了博客,起初使用了sae默认的wordpress 3.4.1,版本实在太低,作为php盲,只好忍了。

后来浏览网站时无心看到了 【WordPress SAE修改版】 WordPress on SAE 更新至 4.1,文章中提供了Wordpress 4.1 SAE改进的下载链接,以及升级方法,简单明了,于是Jackie的博客升级为了4.1版本。

前几天,Jackie在查阅资料时,不小心看到了 WordPress静态化插件SAE版, 于是不安分起来,参照文章中介绍的方法折腾了半天,终于搞定了静态化。

博客静态化的操作步骤

  1. 从 cos-html-cache插件官网下载插件,当前最新版本为2.7.4。将插件解压,并上传压缩包内的文件上传至wp-content/plugins/cos-html-cache内,目录内的文件布局如下:
    common.js.phpcos-html-cache.phpcosbeta-zh_CN.mocosbeta-zh_CN.poreadme-chs.txtreadme.txt
    登录后复制
  2. 在插件目录下增加index.php文件,内容如下
    <?php define("SUMMETA","<! --this is the first view page created at ".date("Y-m-d H:i:s")." by JackieAtHome index.php -->"); $kv = new SaeKV(); $kv->init(); $sitemap = $kv->get($_SERVER['SCRIPT_URI']."index.html"); if ($sitemap) {    header('Content-type:text/html; charset=utf-8');    echo $sitemap; }else{    echo fetchUrl($_SERVER['SCRIPT_URI']).SUMMETA; } function fetchUrl($url){    $ch=curl_init();    curl_setopt($ch, CURLOPT_AUTOREFERER,0);    curl_setopt($ch, CURLOPT_REFERER, 'staticindex');    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    $ret=curl_exec($ch);    curl_close($ch);    if ($ret) {        return $ret;    }else{        return false;    }}?> 
    登录后复制
  3. 在插件目录下增加static.php,内容如下
    <?php define("SUMMETA","<! --this is the first view page created at ".date("Y-m-d H:i:s")." by JackieAtHome static.php  -->"); $kv = new SaeKV(); $kv->init(); $sitemap = $kv->get($_SERVER['SCRIPT_URI']); if ($sitemap) {    header('Content-type:text/html; charset=utf-8');    echo $sitemap; }else{    echo fetchUrl($_SERVER['SCRIPT_URI']).SUMMETA; } function fetchUrl($url){    $ch=curl_init();    curl_setopt($ch, CURLOPT_AUTOREFERER,0);    curl_setopt($ch, CURLOPT_REFERER, 'static');    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    $ret=curl_exec($ch);    curl_close($ch);    if ($ret) {        return $ret;    }else{        return false;    }}?> 
    登录后复制
  4. 修改SAE的config.yaml,增加rewrite规则,内容如下
    name: app_nameversion: app_versionhandle:  - rewrite:  if ( %{REQ:REFERER} != "static" && %{REQUEST_URI} ~ "html$" ) goto "wp-content/plugins/cos-html-cache/static.php"  - rewrite:  if ( !is_dir() && !is_file()) goto "index.php?%{QUERY_STRING}"  - rewrite:  if ( path ~ "/(\s*)$" && %{REQ:REFERER} != "staticindex" ) goto "wp-content/plugins/cos-html-cache/index.php"  
    登录后复制
  5. 修改cos-html-cache.php,使用SaeKV对象来保存、读取数据,如下是Patch
    Index: cos-html-cache.php===================================================================--- cos-html-cache.php  (revision 27)+++ cos-html-cache.php  (revision 28)@@ -84,17 +84,21 @@    }    if ( !strstr( strtolower($Content), '</html>' ) ) return;+   $kv=new SaeKV();+   $kv->init();+   $kv->set($path, $Content);+       //if sql error ignore...-   $fp = @fopen( $path , "w+" );-   if( $fp ){-       @chmod($path, 0666 ) ;-       @flock($fp ,LOCK_EX );+//     $fp = @fopen( $path , "w+" );+//     if( $fp ){+//         @chmod($path, 0666 ) ;+//         @flock($fp ,LOCK_EX );-       // write the file。-       fwrite( $fp , $Content );-       @flock($fp, LOCK_UN);-       fclose($fp);-    }+//         // write the file。+//         fwrite( $fp , $Content );+//         @flock($fp, LOCK_UN);+//         fclose($fp);+//      } } /* read the content from output buffer */@@ -137,16 +141,27 @@    elseif( SCRIPT_URI == CosSiteHome) {// creat homepage-       $fp = @fopen( CosBlogPath."index.bak" , "w+" );-       if( $fp ){-           @flock($fp ,LOCK_EX );-           // write the file。-           fwrite( $fp , $buffer.COSMETA );-           @flock($fp, LOCK_UN);-           fclose($fp);-        }-       if(IS_INDEX)-           @rename(CosBlogPath."index.bak",CosBlogPath."index.html");+       +       $kv=new SaeKV();+       $kv->init();+       if (IS_INDEX) {+           $kv->set(CosBlogPath."index.html", $buffer.COSMETA);+       }+       else {+           $kv->set(CosBlogPath."index.bak", $buffer.COSMETA);+       }+       +               +//         $fp = @fopen( CosBlogPath."index.bak" , "w+" );+//         if( $fp ){+//             @flock($fp ,LOCK_EX );+//             // write the file。+//             fwrite( $fp , $buffer.COSMETA );+//             @flock($fp, LOCK_UN);+//             fclose($fp);+//          }+//         if(IS_INDEX)+//             @rename(CosBlogPath."index.bak",CosBlogPath."index.html");    }    else        CreateHtmlFile($_SERVER['REQUEST_URI'],$buffer.COSMETA );
    登录后复制
  6. 登录博客后台,在 设置-> 固定链接,修改URL格式为 /archives/%post_id%.html,即在末尾增加 .html。
  7. 登录博客后台,打开 插件页面,找到 cos-html-cache插件,点击启用。
  8. 重新刷新首页或者文章页面,查看页面源码,如果页面底部出现如下代码,

    硅基智能
    硅基智能

    基于Web3.0的元宇宙,去中心化的互联网,高质量、沉浸式元宇宙直播平台,用数字化重新定义直播

    硅基智能 62
    查看详情 硅基智能
    <! --this is the first view page created at 2016-04-01 22:35:13 by JackieAtHome index.php --> 
    登录后复制

    或者

    立即学习前端免费学习笔记(深入)”;

    <! --this is the first view page created at 2016-04-01 22:35:13 by JackieAtHome static.php -->
    登录后复制

    恭喜你,博客页面静态化成功。

注意事项

  1. WP-PostViews插件的文章浏览统计特性失效,参考 通过Ajax手动解决WordPress WP-PostViews不计数的问题中的方法,在页面
WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号