Table of Contents
博客静态化的操作步骤
注意事项
参考资料
Home Web Front-end HTML Tutorial 基于cos-html-cache实现WordPress页面纯静态化至KVDB_html/css_WEB-ITnose

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

Jun 21, 2016 am 08:52 AM

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内,目录内的文件布局如下:

    1

    common.js.phpcos-html-cache.phpcosbeta-zh_CN.mocosbeta-zh_CN.poreadme-chs.txtreadme.txt

    Copy after login
  2. 在插件目录下增加index.php文件,内容如下

    1

    <?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;    }}?>

    Copy after login
  3. 在插件目录下增加static.php,内容如下

    1

    <?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;    }}?>

    Copy after login
  4. 修改SAE的config.yaml,增加rewrite规则,内容如下

    1

    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"

    Copy after login
  5. 修改cos-html-cache.php,使用SaeKV对象来保存、读取数据,如下是Patch

    1

    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 );

    Copy after login
  6. 登录博客后台,在 设置-> 固定链接,修改URL格式为 /archives/%post_id%.html,即在末尾增加 .html。
  7. 登录博客后台,打开 插件页面,找到 cos-html-cache插件,点击启用。
  8. 重新刷新首页或者文章页面,查看页面源码,如果页面底部出现如下代码,

    1

    <! --this is the first view page created at 2016-04-01 22:35:13 by JackieAtHome index.php -->

    Copy after login

    或者

    1

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

    Copy after login

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

注意事项

  1. WP-PostViews插件的文章浏览统计特性失效,参考 通过Ajax手动解决WordPress WP-PostViews不计数的问题中的方法,在页面 前增加如下代码。

    1

    <?php if (is_singular()) : ?>    <!-- ajax post view -->    <script type="text/javascript">        $.ajax({            url: "<?php echo get_site_url(). '/wp-admin/admin-ajax.php?>'?>",            data: {                "postviews_id": "<?php the_ID(); ?>",                "action": "postviews",                "_": new Date().getTime()            }        });    </script><?php endif; ?>

    Copy after login
  2. pingback失效,在当前博客主题的header.php中,css引用前增加如下代码。

    1

    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />

    Copy after login

参考资料

  • WordPress静态化插件SAE版
  • wordpress的缓存插件cos-html-cache的源码解读
  • wordpress 中使用自带 admin-ajax.php
  • wordpress自带admin-ajax请求ajax太慢
  • 通过Ajax手动解决WordPress WP-PostViews不计数的问题
  • 【WordPress SAE修改版】 WordPress on SAE 更新至 4.1
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles