typecho完全静态化

前言

众所周知,typecho是一款轻量的博客程序,响应快,占用小,是一款收人喜爱的程序,但是静态化typecho可以让你博客起飞,不仅可以当静态备份使用,还可以优化seo,利于cdn缓存等等 这边会提供3种静态方案,供大家选择:sunglasses:

首页静态化

可以让首页提前生成html文件,对于第一次打开有巨大帮助

<?php
$nowtime=time();
$pastsec = $nowtime - $_GET["t"];
if($pastsec<600)
{
exit; //10分钟更新一次,时间可以自己调整
}
ob_start(); //打开缓冲区
include("index.php");
$content = ob_get_contents(); //得到缓冲区的内容
$content .= "\n<script language=javascript src='/index/thumb.png' data-original=\"html.php?t=".$nowtime."\"></script>"; //加上调用更新程序的代码

file_put_contents("index.html",$content);
if (!function_exists("file_put_contents"))
{
function file_put_contents($fn,$fs)
{
$fp=fopen($fn,"w+");
fputs($fp,$fs);
fclose($fp);  
}
}
?>

将文件保存至 html.php放置首页即可,安装说明另行修改代码即可。

完全静态化

你可以有2台服务器协作完成此操作 1台为html存放服务器(静态服务器) 1台为你的博客服务器(动态服务器)

步骤1 在静态服务器安装php,并创建 php文件,代码参考如下

<?php
$url = 'https://xxxx'; //网址,不能以"/"结尾
$rurl='https://xxxx'; //要替换成路径或网址,可为空,不能以"/"结尾
$dir = __DIR__ . "/" . str_replace('https://', '', str_replace('http://', "", $url));
exec("clear",$clc);
echo $clc[0];
echo "开始下载文件\r\n";
exec("rm -rf {$dir}",$return);
exec("wget -r -p -np {$url}",$return);

$dirs = get_filenamesbydir($dir);

//不处理非html文件
for ($i = 0; $i < count($dirs); $i++) {
    $file=str_replace(__DIR__, "", $dirs[$i]['file']);

    if (!preg_match("/html/js/jpg/png/jpeg/css/",$file)  ) {
        //删除对应的元素
        unset($dirs[$i]);
        
    }
    
}
array_filter($dirs);
sort($dirs);//重新生成索引下标

//网址处理
$count=count($dirs);
for ($i = 0; $i < $count; $i++) {
    $content=str_replace($url,$rurl,file_get_contents($dirs[$i]['file']));
    file_put_contents($dirs[$i]['file'],$content);
    $n=$i+1;
    exec("clear",$clc);
    echo $clc[0];
    echo "文件下载完毕\r\n";
    echo "开始处理文件,共{$count}个文件需要处理,已处理{$n}个\r\n";
    
}
echo "处理完毕,文件目录:{$dir}\r\n";


function get_allfiles($path, &$files)
{
    if (is_dir($path)) {
        $dp = dir($path);
        while ($file = $dp->read()) {
            if ($file !== "." && $file !== "..") {
                get_allfiles($path . "/" . $file, $files);
            }
        }
        $dp->close();
    }
    if (is_file($path)) {
        $files[] = ['file' => $path];
    }
}
function get_filenamesbydir($dir)
{
    $files = array();
    get_allfiles($dir, $files);
    return $files;
}

名为staticpass.php 然后使用 php staticpass.php 会采集html,如果你的博客页面很多,对博客服务器压力比较大,慎重运行,如果采集服务器进IP黑名单,可以设置白名单。

然后你可以安装宝塔或者直接安装web服务器,将运行目录设置在采集完的目录

我的静态采集成功实例 静态web :https://static.fbk.ink 采集对象:https://fbk.ink

拓展使用

对此,你已经可以手动采集你博客页面了,但是,你也可以设置自动化采集 以上为我的自动采集脚本,十分简单,2天运行一次

在图片方面,我的博客在采集过程中发现无法采集文章图片,我认为最好的方案是将文章图片域名单独分开,例如原版图片链接为 https://fbk.ink/usr/upload/xxxx.jpg 你可以在数据库或者插件替换链接为新版图片链接 例如:https://cdn-img.fbk.ink

方案和思路参考至互联网,如果侵权,请联系

相关文章

学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习...
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面...
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生...
Can’t connect to local MySQL server through socket \'/v...
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 ...
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服...