从cos-html-cache插件官网下载插件,当前最新版本为2.7.4。将插件解压,并上传压缩包内的文件上传至wp-content/plugins/cos-html-cache内,目录内的文件布局如下:
common.js.php
cos-html-cache.php
cosbeta-zh_CN.mo
cosbeta-zh_CN.po
readme-chs.txt
readme.txt
在插件目录下增加index.php文件,内容如下
define("SUMMETA","");
$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;
}
}
?>
在插件目录下增加static.php,内容如下
define("SUMMETA","");
$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;
}
}
?>
修改SAE的config.yaml,增加rewrite规则,内容如下
name: app_name
version: app_version
handle:
- 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"
修改cos-html-cache.php,使用SaeKV对象来保存、读取数据,如下是Patch。建议参考项目https://github.com/jackieathome/cos-html-cache-for-sae中的源码。
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), '