首先在网站根目录创建一个名为 build_index.php
的文件 然后复制以下代码进去
<?php
/**
* 首页静态化脚本
* Author: ZiHan
* Blog: zbk52.com
*/
ini_set( 'date.timezone', 'PRC' );
/* 缓存过期时间 单位:秒 */
$expire = 86400;
/* 主动刷新密码 格式:http://zbk52.com/build_index.php?password=123456 */
$password = '123456';
$file_time = @filemtime( 'index.html' );
time() - $file_time > $expire && create_index();
isset( $_GET['password'] ) && $_GET['password'] == $password && create_index();
/**
* 生成 index.html
*/
function create_index()
{
ob_start();
include( 'index.php' );
$content = ob_get_contents();
$content .= "\n<!-- Create time: " . date( 'Y-m-d H:i:s' ) . " -->";
/* 调用更新 */
$content .= "\n<script language=javascript src='build_index.php'></script>";
ob_clean();
$res = file_put_contents( 'index.html', $content );
if ( $res !== false )
{
die( 'Create successful' );
}
else
{
die( 'Create error' );
}
}
1.在站点根目录下创建或上传 build_index.php
,访问这个文件就可以在根目录生成静态文件了。
2.更新缓存,在宝塔定时任务添加访问Url任务,时间最好更新缓存一天一次http://www.awjds.cn/build_index.php?password=123456
可以在脚本里面设置你的密码,防止被他人利用发起CC攻击,频繁写文件造成服务器IO过高。
3.如果不想使用过期更新,可以从脚本里面去掉调用更新那句 script
代码,缓存过期时间修改 $expire
变量。
4.另外需要注意的是你的 index.html
要在 index.php
前面,否则不生效。Apache
修改 DirectoryIndex
, Nginx
修改 index
,IIS
配置默认文档。
访问成功一次后 回到宝塔
将index.html
放到前面去 然后访问你的网站
本文共 249 个字数,平均阅读时长 ≈ 1分钟
评论 (0)