置业安家帮手新房二手房刘芳 發表於 2023-12-21 00:00:00

WordPress在SAE平台实现的静态化插件及用法示例

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
本文实例讲述了WordPress在SAE平台实现的静态化插件及用法。分享给大家供大家参考,具体如下:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
WordPress是一款非常热门的博客系统,但是其对于静态化的支持却不是很好。虽然说可以配置主机的htaccess规则,来达到伪静态的效果,但是如果能够纯静态,提速效果会非常明显,而且体验也好。而且很多人都觉得,静态化对SEO也有很好的帮助。而纯静态的首选方法就是使用插件,WordPress有着好几款静态化的插件,但是对于SAE的特殊环境来说,都是无能为力。今天就借着一款叫做cos-html-cache的插件,来实现把文章页面和首页都静态化到KVDB中。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
由于SAE是全分布式的,所以禁止了写入功能,所以移植的第一步就是把写入到本地的全部改写到KVDB中。分别有五处地方需要修改:<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
$kv=new SaeKV();<br>
$kv-&gt;init();<br>
$kv-&gt;set($path,$Content);<br>
//if sql error ignore...<br>
// $fp = @fopen( $path , "w+" );<br>
// if( $fp ){<br>
// @chmod($path, 0666 ) ;<br>
// @flock($fp ,LOCK_EX );<br>
// fwrite( $fp , $Content );<br>
// @flock($fp, LOCK_UN);<br>
// fclose($fp);<br>
// }</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
把五处根据代码都修改为写入KVDB中即可,这里使用你的url作为存储的key。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
但是这样还不行,你只是写入了KVDB,去访问的时候还得要读取出来,这时就需要配置rewrite了。在你应用的AppConfig中添加以下三行:<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode6" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
handle:<br>
- rewrite: if ( %{REQ:REFERER} != "static" &amp;&amp; %{REQUEST_URI} ~ "html$" ) goto "wp-content/plugins/cos-html-cache/static.php"<br>
- rewrite: if ( !is_dir() &amp;&amp; !is_file()) goto "index.php?%{QUERY_STRING}"<br>
- rewrite: if ( path ~ "/(\s*)$" &amp;&amp; %{REQ:REFERER} != "staticindex" ) goto "wp-content/plugins/cos-html-cache/index.php"</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
注意的是,伪静态的规则一定要在另外两个规则的中间,不然很容易出问题的。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
然后再到cos的插件目录,新建两个文件index.php和static.php,分别用来处理首页和文章页的重定向输出,写入以下代码:<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode7" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
define("SUMMETA","&lt;! --this is the first view page created at ".date("Y-m-d H:i:s")." by summer --&gt;");<br>
$kv = new SaeKV();<br>
$kv-&gt;init();<br>
$sitemap = $kv-&gt;get($_SERVER['SCRIPT_URI']);<br>
if ($sitemap) {<br>
header('Content-type:text/html; charset=utf-8');<br>
echo $sitemap;<br>
}else{<br>
echo fetchUrl($_SERVER['SCRIPT_URI']).SUMMETA;<br>
}<br>
function fetchUrl($url){<br>
$ch=curl_init();<br>
curl_setopt($ch, CURLOPT_AUTOREFERER,0);<br>
curl_setopt($ch, CURLOPT_REFERER, 'static');<br>
curl_setopt($ch, CURLOPT_URL, $url);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>
$ret=curl_exec($ch);<br>
curl_close($ch);<br>
if ($ret) {<br>
return $ret;<br>
}else{<br>
return false;<br>
}<br>
}</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'> </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>上面的是static.php的,主要作用就是把KVDB中的内容输出,若不存在则向后端程序去请求页面,并且生成静态的到KVDB中。index.php的和上面类似,只要在get的键后面加上/index.html,然后把REFERER的值改为staticindex即可。</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
还有就是要注意,你的博客必须已经是伪静态的了,才可以使用该插件,不然是没有任何效果的。而且纯静态之后,文章的访问统计以及随机文章之类的都会失效的,解决办法就是使用javascript统计,然后使用ajax请求数据。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
这样,就可以到后台去启用该插件了。那如何判定是否有生成静态页面的呢?可以在访问的时候查看源代码,拉到最底下看是否有生成一行插件的标记即可。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
2015-03-31更新:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
1.解决首页问题</p>
<img title="WordPress在SAE平台实现的静态化插件及用法示例" alt="WordPress在SAE平台实现的静态化插件及用法示例" src="https://zhuji.jb51.net/uploads/img/202305/a7919d3db50b22377036dce9fc090582.jpg" style="max-width:100%!important;height:auto!important;border: 1px solid rgb(204, 204, 204); vertical-align: middle; padding: 1px; overflow: hidden; max-width: 696px; font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px; width: 600px; height: 261px;'><p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
插件点击此处<strong><font color="#ff0000" style="line-height: 25.2px;">本站下载</font></strong>。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
2015-06-23更新:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
1.解决首页搜索跳转</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
BUG描述:当网站有搜索功能时,由于搜索的时候是带有s参数的,而插件并未判断该参数,导致搜索时会一直循环跳转到首页<br>
解决:加入判断即可</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
希望本文所述对大家基于wordpress的程序设计有所帮助。</p>
頁: [1]
查看完整版本: WordPress在SAE平台实现的静态化插件及用法示例