本文主要和大家介绍php 实现页面静态化的几种方法,需要的朋友可以参考下,希望能帮助到大家。
1、通过buffer来实现
需要用file_put_contents ob_get_clean()等内置函数
ob_start ();
include "filterpost.html";
$mtime = filemtime("./filterpost.html");//在这里可以判断文件是否存在和过期,然后做缓存或者生成静态文件操作
$pagecache = str_replace('submit2','login',ob_get_contents());//将缓存去中的内容替换
ob_end_clean();
echo $mtime;
echo $pagecache;
2、通过$_server['path_info']来实现
echo '<pre>';
print_r($_server);
preg_match('/^\/(\d+)\/(\d+)\.html/',$_server['path_info'],$arr);
print_r($arr);
3、通过apache配置来实现
需要开启rewrite重写模块
通过rewrite来配置vhost
rewriteengine on
rewritecond %{document_root}%{request_filename} !-d
rewritecond %{document_root}%{request_filename} !-f
rewriterule ^/detail/([0-9]*).html$ /detail.php?id=$1
如果服务器下不存在文件夹及其文件,那么就重写定义到/detail.php
http://localhost/detail/1.html
如果没有detail文件夹下的1.html 那么就重写定义到./detail.php
4、通过nginx配置来实现
在nginx.conf中配置
rewrite ^/detail/(\d+)\.html$ /detail.php?id=$1 last;
当然建议大家参考一些比较成熟的cms的方法,对于页面数量不大的话,第一种方法还是不错的。
相关推荐:
thinkphp3.2.3页面静态化实现方法
html实现页面静态化的案例
php实现页面静态化视频教程资料推荐
以上就是php页面静态化的实现代码的详细内容。