wordpress生成纯静态首页缓存

为了加快首页加载,不得不将首页缓存起来了,直接纯静态化缓存生成html页面。
将下面的代码保存为一个php文件,文件名随意,比如:creat_index.php

<?php
if(file_exists("index.html"))
{
unlink("index.html");
}
$baseCmsUrl = "https://www.fankuiba.com";   //这里改为你网站的根目录
$dmPageName = "index.php";
$stPageName = "index.html";
$tureStFile = dirname(__FILE__).'/'.$stPageName;
{
$body = file_get_contents($baseCmsUrl.'/'.$dmPageName);
$fp = fopen($tureStFile, 'w');
fwrite($fp, $body);
fclose($fp);
}
header("Location:$baseCmsUrl/index.html");
?>

保存好之后,将creat_index.php上传到网站的根目录,
然后在浏览器上访问这个文件 就会在网站根目录生成一个index.html文件
比如JV上传好后,访问https://www.fankuiba.com/creat_index.php ,就能生成一个index.html
这时,无论我们访问 https://www.fankuiba.com 还是 https://www.fankuiba.com/index.html 都能访问自己网站的首页 。

但是这对搜索引擎不利,我们必须把后面的 index.html 小尾巴去掉,
也就是输入 https://www.fankuiba.com/index.html 时 301自动跳转为 https://www.fankuiba.com
解决方法是将 下列代码写入 .htaccess 文件中 :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.(php|html|htm) HTTP/
RewriteRule ^index.(php|html|htm)$ https://www.fankuiba.com/ [R=301,L]
</IfModule>

(注意修改代码中的网站地址)
添加好之后,生成的index.html就没有什么大问题了,当我们更新文章之后可以再删掉它 重新生成一次!
如此一来首页加载速度就会快很多。最后感谢v7v3.com提供的代码。

wordpress生成首页缓存

wordpress生成首页缓存

赞(0)