yuyu5 发表于 2017-6-8 10:28:59

上课了讲解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/(*).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;

小甲鱼 发表于 2017-6-8 23:26:44

{:10_256:} 注意排版,其实 WEB 学习是非常注重排版强迫症的……
页: [1]
查看完整版本: 上课了讲解PHP实现页面静态化